Mercurial > p > roundup > code
comparison roundup/mailer.py @ 1830:4ac11e7fa11a
Fix mailer bug [SF#817470]...
...and add docstrings to prevent this from happening again.
| author | Johannes Gijsbers <jlgijsbers@users.sourceforge.net> |
|---|---|
| date | Sat, 04 Oct 2003 11:21:47 +0000 |
| parents | ee33ce4987f5 |
| children | ce6806a2a72d |
comparison
equal
deleted
inserted
replaced
| 1829:4e4afbe3cd30 | 1830:4ac11e7fa11a |
|---|---|
| 1 """Sending Roundup-specific mail over SMTP.""" | 1 """Sending Roundup-specific mail over SMTP.""" |
| 2 # $Id: mailer.py,v 1.2 2003-09-08 21:08:59 jlgijsbers Exp $ | 2 # $Id: mailer.py,v 1.3 2003-10-04 11:21:47 jlgijsbers Exp $ |
| 3 | 3 |
| 4 import time, quopri, os, socket, smtplib, re | 4 import time, quopri, os, socket, smtplib, re |
| 5 | 5 |
| 6 from cStringIO import StringIO | 6 from cStringIO import StringIO |
| 7 from MimeWriter import MimeWriter | 7 from MimeWriter import MimeWriter |
| 25 author = straddr((self.config.TRACKER_NAME, | 25 author = straddr((self.config.TRACKER_NAME, |
| 26 self.config.ADMIN_EMAIL)) | 26 self.config.ADMIN_EMAIL)) |
| 27 message = StringIO() | 27 message = StringIO() |
| 28 writer = MimeWriter(message) | 28 writer = MimeWriter(message) |
| 29 writer.addheader('Subject', encode_header(subject)) | 29 writer.addheader('Subject', encode_header(subject)) |
| 30 writer.addheader('To', to) | 30 writer.addheader('To', ', '.join(to)) |
| 31 writer.addheader('From', author) | 31 writer.addheader('From', author) |
| 32 writer.addheader('Date', time.strftime("%a, %d %b %Y %H:%M:%S +0000", | 32 writer.addheader('Date', time.strftime("%a, %d %b %Y %H:%M:%S +0000", |
| 33 time.gmtime())) | 33 time.gmtime())) |
| 34 | 34 |
| 35 # Add a unique Roundup header to help filtering | 35 # Add a unique Roundup header to help filtering |
| 40 writer.addheader('MIME-Version', '1.0') | 40 writer.addheader('MIME-Version', '1.0') |
| 41 | 41 |
| 42 return message, writer | 42 return message, writer |
| 43 | 43 |
| 44 def standard_message(self, to, subject, content, author=None): | 44 def standard_message(self, to, subject, content, author=None): |
| 45 """Send a standard message. | |
| 46 | |
| 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 """ | |
| 45 message, writer = self.get_standard_message(to, subject, author) | 53 message, writer = self.get_standard_message(to, subject, author) |
| 46 | 54 |
| 47 writer.addheader('Content-Transfer-Encoding', 'quoted-printable') | 55 writer.addheader('Content-Transfer-Encoding', 'quoted-printable') |
| 48 body = writer.startbody('text/plain; charset=utf-8') | 56 body = writer.startbody('text/plain; charset=utf-8') |
| 49 content = StringIO(content) | 57 content = StringIO(content) |
| 51 | 59 |
| 52 self.smtp_send(to, message) | 60 self.smtp_send(to, message) |
| 53 | 61 |
| 54 def bounce_message(self, bounced_message, to, error, | 62 def bounce_message(self, bounced_message, to, error, |
| 55 subject='Failed issue tracker submission'): | 63 subject='Failed issue tracker submission'): |
| 56 message, writer = self.get_standard_message(', '.join(to), subject) | 64 """Bounce a message, attaching the failed submission. |
| 65 | |
| 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) | |
| 57 | 74 |
| 58 part = writer.startmultipartbody('mixed') | 75 part = writer.startmultipartbody('mixed') |
| 59 part = writer.nextpart() | 76 part = writer.nextpart() |
| 60 part.addheader('Content-Transfer-Encoding', 'quoted-printable') | 77 part.addheader('Content-Transfer-Encoding', 'quoted-printable') |
| 61 body = part.startbody('text/plain; charset=utf-8') | 78 body = part.startbody('text/plain; charset=utf-8') |
| 81 writer.lastpart() | 98 writer.lastpart() |
| 82 | 99 |
| 83 self.smtp_send(to, message) | 100 self.smtp_send(to, message) |
| 84 | 101 |
| 85 def smtp_send(self, to, message): | 102 def smtp_send(self, to, message): |
| 103 """Send a message over SMTP, using roundup's config. | |
| 104 | |
| 105 Arguments: | |
| 106 - to: a list of addresses usable by rfc822.parseaddr(). | |
| 107 - message: a StringIO instance with a full message. | |
| 108 """ | |
| 86 if self.debug: | 109 if self.debug: |
| 87 # don't send - just write to a file | 110 # don't send - just write to a file |
| 88 open(self.debug, 'a').write('FROM: %s\nTO: %s\n%s\n' % | 111 open(self.debug, 'a').write('FROM: %s\nTO: %s\n%s\n' % |
| 89 (self.config.ADMIN_EMAIL, | 112 (self.config.ADMIN_EMAIL, |
| 90 ', '.join(to), | 113 ', '.join(to), |
| 93 # now try to send the message | 116 # now try to send the message |
| 94 try: | 117 try: |
| 95 # send the message as admin so bounces are sent there | 118 # send the message as admin so bounces are sent there |
| 96 # instead of to roundup | 119 # instead of to roundup |
| 97 smtp = SMTPConnection(self.config) | 120 smtp = SMTPConnection(self.config) |
| 98 smtp.sendmail(self.config.ADMIN_EMAIL, [to], | 121 smtp.sendmail(self.config.ADMIN_EMAIL, to, |
| 99 message.getvalue()) | 122 message.getvalue()) |
| 100 except socket.error, value: | 123 except socket.error, value: |
| 101 raise MessageSendError("Error: couldn't send email: " | 124 raise MessageSendError("Error: couldn't send email: " |
| 102 "mailhost %s"%value) | 125 "mailhost %s"%value) |
| 103 except smtplib.SMTPException, msg: | 126 except smtplib.SMTPException, msg: |
