93ef7f9ba5d26b3517014c86c42ec292f3a6bf3e
1 """Sending Roundup-specific mail over SMTP.
2 """
3 __docformat__ = 'restructuredtext'
4 # $Id: mailer.py,v 1.7 2004-02-29 00:35:55 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().
96 - error: the reason of failure as a string.
97 - subject: the subject as a string.
99 """
100 message, writer = self.get_standard_message(to, subject)
102 part = writer.startmultipartbody('mixed')
103 part = writer.nextpart()
104 part.addheader('Content-Transfer-Encoding', 'quoted-printable')
105 body = part.startbody('text/plain; charset=utf-8')
106 body.write('\n'.join(error))
108 # attach the original message to the returned message
109 part = writer.nextpart()
110 part.addheader('Content-Disposition', 'attachment')
111 part.addheader('Content-Description', 'Message you sent')
112 body = part.startbody('text/plain')
114 for header in bounced_message.headers:
115 body.write(header)
116 body.write('\n')
117 try:
118 bounced_message.rewindbody()
119 except IOError, message:
120 body.write("*** couldn't include message body: %s ***"
121 % bounced_message)
122 else:
123 body.write(bounced_message.fp.read())
125 writer.lastpart()
127 self.smtp_send(to, message)
129 def smtp_send(self, to, message):
130 """Send a message over SMTP, using roundup's config.
132 Arguments:
133 - to: a list of addresses usable by rfc822.parseaddr().
134 - message: a StringIO instance with a full message.
135 """
136 if self.debug:
137 # don't send - just write to a file
138 open(self.debug, 'a').write('FROM: %s\nTO: %s\n%s\n' %
139 (self.config.ADMIN_EMAIL,
140 ', '.join(to),
141 message.getvalue()))
142 else:
143 # now try to send the message
144 try:
145 # send the message as admin so bounces are sent there
146 # instead of to roundup
147 smtp = SMTPConnection(self.config)
148 smtp.sendmail(self.config.ADMIN_EMAIL, to,
149 message.getvalue())
150 except socket.error, value:
151 raise MessageSendError("Error: couldn't send email: "
152 "mailhost %s"%value)
153 except smtplib.SMTPException, msg:
154 raise MessageSendError("Error: couldn't send email: %s"%msg)
156 class SMTPConnection(smtplib.SMTP):
157 ''' Open an SMTP connection to the mailhost specified in the config
158 '''
159 def __init__(self, config):
161 smtplib.SMTP.__init__(self, config.MAILHOST)
163 # use TLS?
164 use_tls = getattr(config, 'MAILHOST_TLS', 'no')
165 if use_tls == 'yes':
166 # do we have key files too?
167 keyfile = getattr(config, 'MAILHOST_TLS_KEYFILE', '')
168 if keyfile:
169 certfile = getattr(config, 'MAILHOST_TLS_CERTFILE', '')
170 if certfile:
171 args = (keyfile, certfile)
172 else:
173 args = (keyfile, )
174 else:
175 args = ()
176 # start the TLS
177 self.starttls(*args)
179 # ok, now do we also need to log in?
180 mailuser = getattr(config, 'MAILUSER', None)
181 if mailuser:
182 self.login(*config.MAILUSER)
184 # use the 'email' module, either imported, or our copied version
185 try :
186 from email.Utils import formataddr as straddr
187 except ImportError :
188 # code taken from the email package 2.4.3
189 def straddr(pair, specialsre = re.compile(r'[][\()<>@,:;".]'),
190 escapesre = re.compile(r'[][\()"]')):
191 name, address = pair
192 if name:
193 quotes = ''
194 if specialsre.search(name):
195 quotes = '"'
196 name = escapesre.sub(r'\\\g<0>', name)
197 return '%s%s%s <%s>' % (quotes, name, quotes, address)
198 return address