Code

documentation cleanup
[roundup.git] / roundup / mailer.py
1 """Sending Roundup-specific mail over SMTP.
2 """
3 __docformat__ = 'restructuredtext'
4 # $Id: mailer.py,v 1.5 2004-02-11 23:55:08 richard Exp $
6 import time, quopri, os, socket, smtplib, re
8 from cStringIO import StringIO
9 from MimeWriter import MimeWriter
11 from roundup.rfc2822 import encode_header
12 from roundup import __version__
14 class MessageSendError(RuntimeError):
15     pass
17 class Mailer:
18     """Roundup-specific mail sending."""
19     def __init__(self, config):
20         self.config = config
22         # set to indicate to roundup not to actually _send_ email
23         # this var must contain a file to write the mail to
24         self.debug = os.environ.get('SENDMAILDEBUG', '')
26     def get_standard_message(self, to, subject, author=None):
27         if not author:
28             author = straddr((self.config.TRACKER_NAME,
29                               self.config.ADMIN_EMAIL))
30         message = StringIO()
31         writer = MimeWriter(message)
32         writer.addheader('Subject', encode_header(subject))
33         writer.addheader('To', ', '.join(to))
34         writer.addheader('From', author)
35         writer.addheader('Date', time.strftime("%a, %d %b %Y %H:%M:%S +0000",
36                                                time.gmtime()))
38         # Add a unique Roundup header to help filtering
39         writer.addheader('X-Roundup-Name', self.config.TRACKER_NAME)
40         # and another one to avoid loops
41         writer.addheader('X-Roundup-Loop', 'hello')
42         # finally, an aid to debugging problems
43         writer.addheader('X-Roundup-Version', __version__)
45         writer.addheader('MIME-Version', '1.0')       
46         
47         return message, writer
49     def standard_message(self, to, subject, content, author=None):
50         """Send a standard message.
52         Arguments:
53         - to: a list of addresses usable by rfc822.parseaddr().
54         - subject: the subject as a string.
55         - content: the body of the message as a string.
56         - author: the sender as a string, suitable for a 'From:' header.
57         """
58         message, writer = self.get_standard_message(to, subject, author)
60         writer.addheader('Content-Transfer-Encoding', 'quoted-printable')
61         body = writer.startbody('text/plain; charset=utf-8')
62         content = StringIO(content)
63         quopri.encode(content, body, 0)
65         self.smtp_send(to, message)
67     def bounce_message(self, bounced_message, to, error,
68                        subject='Failed issue tracker submission'):
69         """Bounce a message, attaching the failed submission.
71         Arguments:
72         - bounced_message: an RFC822 Message object.
73         - to: a list of addresses usable by rfc822.parseaddr().
74         - error: the reason of failure as a string.
75         - subject: the subject as a string.
76         
77         """
78         message, writer = self.get_standard_message(to, subject)
80         part = writer.startmultipartbody('mixed')
81         part = writer.nextpart()
82         part.addheader('Content-Transfer-Encoding', 'quoted-printable')
83         body = part.startbody('text/plain; charset=utf-8')
84         body.write('\n'.join(error))
86         # attach the original message to the returned message
87         part = writer.nextpart()
88         part.addheader('Content-Disposition', 'attachment')
89         part.addheader('Content-Description', 'Message you sent')
90         body = part.startbody('text/plain')
92         for header in bounced_message.headers:
93             body.write(header)
94         body.write('\n')
95         try:
96             bounced_message.rewindbody()
97         except IOError, message:
98             body.write("*** couldn't include message body: %s ***"
99                        % bounced_message)
100         else:
101             body.write(bounced_message.fp.read())
103         writer.lastpart()
105         self.smtp_send(to, message)
106         
107     def smtp_send(self, to, message):
108         """Send a message over SMTP, using roundup's config.
110         Arguments:
111         - to: a list of addresses usable by rfc822.parseaddr().
112         - message: a StringIO instance with a full message.
113         """
114         if self.debug:
115             # don't send - just write to a file
116             open(self.debug, 'a').write('FROM: %s\nTO: %s\n%s\n' %
117                                         (self.config.ADMIN_EMAIL,
118                                          ', '.join(to),
119                                          message.getvalue()))
120         else:
121             # now try to send the message
122             try:
123                 # send the message as admin so bounces are sent there
124                 # instead of to roundup
125                 smtp = SMTPConnection(self.config)
126                 smtp.sendmail(self.config.ADMIN_EMAIL, to,
127                               message.getvalue())
128             except socket.error, value:
129                 raise MessageSendError("Error: couldn't send email: "
130                                        "mailhost %s"%value)
131             except smtplib.SMTPException, msg:
132                 raise MessageSendError("Error: couldn't send email: %s"%msg)
134 class SMTPConnection(smtplib.SMTP):
135     ''' Open an SMTP connection to the mailhost specified in the config
136     '''
137     def __init__(self, config):
138         
139         smtplib.SMTP.__init__(self, config.MAILHOST)
141         # use TLS?
142         use_tls = getattr(config, 'MAILHOST_TLS', 'no')
143         if use_tls == 'yes':
144             # do we have key files too?
145             keyfile = getattr(config, 'MAILHOST_TLS_KEYFILE', '')
146             if keyfile:
147                 certfile = getattr(config, 'MAILHOST_TLS_CERTFILE', '')
148                 if certfile:
149                     args = (keyfile, certfile)
150                 else:
151                     args = (keyfile, )
152             else:
153                 args = ()
154             # start the TLS
155             self.starttls(*args)
157         # ok, now do we also need to log in?
158         mailuser = getattr(config, 'MAILUSER', None)
159         if mailuser:
160             self.login(*config.MAILUSER)
162 # use the 'email' module, either imported, or our copied version
163 try :
164     from email.Utils import formataddr as straddr
165 except ImportError :
166     # code taken from the email package 2.4.3
167     def straddr(pair, specialsre = re.compile(r'[][\()<>@,:;".]'),
168             escapesre = re.compile(r'[][\()"]')):
169         name, address = pair
170         if name:
171             quotes = ''
172             if specialsre.search(name):
173                 quotes = '"'
174             name = escapesre.sub(r'\\\g<0>', name)
175             return '%s%s%s <%s>' % (quotes, name, quotes, address)
176         return address