1 """Sending Roundup-specific mail over SMTP.
2 """
3 __docformat__ = 'restructuredtext'
4 # $Id: mailer.py,v 1.9 2004-03-25 22:53:26 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 '''Form a standard email message from Roundup.
29 "to" - recipients list
30 "subject" - Subject
31 "author" - (name, address) tuple or None for admin email
33 Subject and author are encoded using the EMAIL_CHARSET from the
34 config (default UTF-8).
36 Returns a Message object and body part writer.
37 '''
38 # encode header values if they need to be
39 charset = getattr(self.config, 'EMAIL_CHARSET', 'utf-8')
40 tracker_name = self.config.TRACKER_NAME
41 if charset != 'utf-8':
42 tracker = unicode(tracker_name, 'utf-8').encode(charset)
43 if not author:
44 author = straddr((tracker_name, self.config.ADMIN_EMAIL))
45 else:
46 name = author[0]
47 if charset != 'utf-8':
48 name = unicode(name, 'utf-8').encode(charset)
49 author = straddr((encode_header(name, charset), author[1]))
51 message = StringIO()
52 writer = MimeWriter(message)
53 writer.addheader('Subject', encode_header(subject, charset))
54 writer.addheader('To', ', '.join(to))
55 writer.addheader('From', author)
56 writer.addheader('Date', time.strftime("%a, %d %b %Y %H:%M:%S +0000",
57 time.gmtime()))
59 # Add a unique Roundup header to help filtering
60 writer.addheader('X-Roundup-Name', encode_header(tracker_name,
61 charset))
62 # and another one to avoid loops
63 writer.addheader('X-Roundup-Loop', 'hello')
64 # finally, an aid to debugging problems
65 writer.addheader('X-Roundup-Version', __version__)
67 writer.addheader('MIME-Version', '1.0')
69 return message, writer
71 def standard_message(self, to, subject, content, author=None):
72 """Send a standard message.
74 Arguments:
75 - to: a list of addresses usable by rfc822.parseaddr().
76 - subject: the subject as a string.
77 - content: the body of the message as a string.
78 - author: the sender as a (name, address) tuple
79 """
80 message, writer = self.get_standard_message(to, subject, author)
82 writer.addheader('Content-Transfer-Encoding', 'quoted-printable')
83 body = writer.startbody('text/plain; charset=utf-8')
84 content = StringIO(content)
85 quopri.encode(content, body, 0)
87 self.smtp_send(to, message)
89 def bounce_message(self, bounced_message, to, error,
90 subject='Failed issue tracker submission'):
91 """Bounce a message, attaching the failed submission.
93 Arguments:
94 - bounced_message: an RFC822 Message object.
95 - to: a list of addresses usable by rfc822.parseaddr(). Might be
96 extended or overridden according to the config
97 ERROR_MESSAGES_TO setting.
98 - error: the reason of failure as a string.
99 - subject: the subject as a string.
101 """
102 # see whether we should send to the dispatcher or not
103 dispatcher_email = getattr(self.config, "DISPATCHER_EMAIL",
104 getattr(self.config, "ADMIN_EMAIL"))
105 error_messages_to = getattr(self.config, "ERROR_MESSAGES_TO", "user")
106 if error_messages_to == "dispatcher":
107 to = [dispatcher_email]
108 elif error_messages_to == "both":
109 to.append(dispatcher_email)
111 message, writer = self.get_standard_message(to, subject)
113 part = writer.startmultipartbody('mixed')
114 part = writer.nextpart()
115 part.addheader('Content-Transfer-Encoding', 'quoted-printable')
116 body = part.startbody('text/plain; charset=utf-8')
117 body.write('\n'.join(error))
119 # attach the original message to the returned message
120 part = writer.nextpart()
121 part.addheader('Content-Disposition', 'attachment')
122 part.addheader('Content-Description', 'Message you sent')
123 body = part.startbody('text/plain')
125 for header in bounced_message.headers:
126 body.write(header)
127 body.write('\n')
128 try:
129 bounced_message.rewindbody()
130 except IOError, message:
131 body.write("*** couldn't include message body: %s ***"
132 % bounced_message)
133 else:
134 body.write(bounced_message.fp.read())
136 writer.lastpart()
138 self.smtp_send(to, message)
140 def smtp_send(self, to, message):
141 """Send a message over SMTP, using roundup's config.
143 Arguments:
144 - to: a list of addresses usable by rfc822.parseaddr().
145 - message: a StringIO instance with a full message.
146 """
147 if self.debug:
148 # don't send - just write to a file
149 open(self.debug, 'a').write('FROM: %s\nTO: %s\n%s\n' %
150 (self.config.ADMIN_EMAIL,
151 ', '.join(to),
152 message.getvalue()))
153 else:
154 # now try to send the message
155 try:
156 # send the message as admin so bounces are sent there
157 # instead of to roundup
158 smtp = SMTPConnection(self.config)
159 smtp.sendmail(self.config.ADMIN_EMAIL, to,
160 message.getvalue())
161 except socket.error, value:
162 raise MessageSendError("Error: couldn't send email: "
163 "mailhost %s"%value)
164 except smtplib.SMTPException, msg:
165 raise MessageSendError("Error: couldn't send email: %s"%msg)
167 class SMTPConnection(smtplib.SMTP):
168 ''' Open an SMTP connection to the mailhost specified in the config
169 '''
170 def __init__(self, config):
172 smtplib.SMTP.__init__(self, config.MAILHOST)
174 # use TLS?
175 use_tls = getattr(config, 'MAILHOST_TLS', 'no')
176 if use_tls == 'yes':
177 # do we have key files too?
178 keyfile = getattr(config, 'MAILHOST_TLS_KEYFILE', '')
179 if keyfile:
180 certfile = getattr(config, 'MAILHOST_TLS_CERTFILE', '')
181 if certfile:
182 args = (keyfile, certfile)
183 else:
184 args = (keyfile, )
185 else:
186 args = ()
187 # start the TLS
188 self.starttls(*args)
190 # ok, now do we also need to log in?
191 mailuser = getattr(config, 'MAILUSER', None)
192 if mailuser:
193 self.login(*config.MAILUSER)
195 # use the 'email' module, either imported, or our copied version
196 try :
197 from email.Utils import formataddr as straddr
198 except ImportError :
199 # code taken from the email package 2.4.3
200 def straddr(pair, specialsre = re.compile(r'[][\()<>@,:;".]'),
201 escapesre = re.compile(r'[][\()"]')):
202 name, address = pair
203 if name:
204 quotes = ''
205 if specialsre.search(name):
206 quotes = '"'
207 name = escapesre.sub(r'\\\g<0>', name)
208 return '%s%s%s <%s>' % (quotes, name, quotes, address)
209 return address