Code

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