Code

Attempt to generate more human-readable addresses in email
[roundup.git] / roundup / mailer.py
index 93ef7f9ba5d26b3517014c86c42ec292f3a6bf3e..729aaeab4d7200fbef196db904d53497f59fbde4 100644 (file)
@@ -1,19 +1,45 @@
 """Sending Roundup-specific mail over SMTP.
 """
 __docformat__ = 'restructuredtext'
-# $Id: mailer.py,v 1.7 2004-02-29 00:35:55 richard Exp $
 
-import time, quopri, os, socket, smtplib, re
+import time, quopri, os, socket, smtplib, re, sys, traceback, email
 
 from cStringIO import StringIO
-from MimeWriter import MimeWriter
 
-from roundup.rfc2822 import encode_header
 from roundup import __version__
+from roundup.date import get_timezone
+
+from email.Utils import formatdate, formataddr, specialsre, escapesre
+from email.Message import Message
+from email.Header import Header
+from email.MIMEText import MIMEText
+from email.MIMEMultipart import MIMEMultipart
 
 class MessageSendError(RuntimeError):
     pass
 
+def encode_quopri(msg):
+    orig = msg.get_payload()
+    encdata = quopri.encodestring(orig)
+    msg.set_payload(encdata)
+    del msg['Content-Transfer-Encoding']
+    msg['Content-Transfer-Encoding'] = 'quoted-printable'
+
+def nice_sender_header(name, address, charset):
+    # construct an address header so it's as human-readable as possible
+    # even in the presence of a non-ASCII name part
+    h = Header(charset=charset)
+    # the important bits of formataddr()
+    if specialsre.search(name):
+        name = '"%s"'%escapesre.sub(r'\\\g<0>', name)
+    try:
+        name.encode('ASCII')
+        h.append(name, 'ASCII')
+    except UnicodeEncodeError:
+        h.append(name)
+    h.append('<%s>'%address, 'ASCII')
+    return str(h)
+
 class Mailer:
     """Roundup-specific mail sending."""
     def __init__(self, config):
@@ -21,9 +47,20 @@ class Mailer:
 
         # set to indicate to roundup not to actually _send_ email
         # this var must contain a file to write the mail to
-        self.debug = os.environ.get('SENDMAILDEBUG', '')
-
-    def get_standard_message(self, to, subject, author=None):
+        self.debug = os.environ.get('SENDMAILDEBUG', '') \
+            or config["MAIL_DEBUG"]
+
+        # set timezone so that things like formatdate(localtime=True)
+        # use the configured timezone
+        # apparently tzset doesn't exist in python under Windows, my bad.
+        # my pathetic attempts at googling a Windows-solution failed
+        # so if you're on Windows your mail won't use your configured
+        # timezone.
+        if hasattr(time, 'tzset'):
+            os.environ['TZ'] = get_timezone(self.config.TIMEZONE).tzname(None)
+            time.tzset()
+
+    def get_standard_message(self, to, subject, author=None, multipart=False):
         '''Form a standard email message from Roundup.
 
         "to"      - recipients list
@@ -33,40 +70,47 @@ class Mailer:
         Subject and author are encoded using the EMAIL_CHARSET from the
         config (default UTF-8).
 
-        Returns a Message object and body part writer.
+        Returns a Message object.
         '''
         # encode header values if they need to be
         charset = getattr(self.config, 'EMAIL_CHARSET', 'utf-8')
-        tracker_name = self.config.TRACKER_NAME
-        if charset != 'utf-8':
-            tracker = unicode(tracker_name, 'utf-8').encode(charset)
+        tracker_name = unicode(self.config.TRACKER_NAME, 'utf-8')
         if not author:
-            author = straddr((tracker_name, self.config.ADMIN_EMAIL))
-        else:
+            author = (tracker_name, self.config.ADMIN_EMAIL)
             name = author[0]
-            if charset != 'utf-8':
-                name = unicode(name, 'utf-8').encode(charset)
-            author = straddr((encode_header(name, charset), author[1]))
-
-        message = StringIO()
-        writer = MimeWriter(message)
-        writer.addheader('Subject', encode_header(subject, charset))
-        writer.addheader('To', ', '.join(to))
-        writer.addheader('From', author)
-        writer.addheader('Date', time.strftime("%a, %d %b %Y %H:%M:%S +0000",
-                                               time.gmtime()))
+        else:
+            name = unicode(author[0], 'utf-8')
+        author = nice_sender_header(name, author[1], charset)
+
+        if multipart:
+            message = MIMEMultipart()
+        else:
+            message = MIMEText("")
+            message.set_charset(charset)
+
+        try:
+            message['Subject'] = subject.encode('ascii')
+        except UnicodeError:
+            message['Subject'] = Header(subject, charset)
+        message['To'] = ', '.join(to)
+        message['From'] = author
+        message['Date'] = formatdate(localtime=True)
+
+        # add a Precedence header so autoresponders ignore us
+        message['Precedence'] = 'bulk'
 
         # Add a unique Roundup header to help filtering
-        writer.addheader('X-Roundup-Name', encode_header(tracker_name,
-            charset))
+        try:
+            message['X-Roundup-Name'] = tracker_name.encode('ascii')
+        except UnicodeError:
+            message['X-Roundup-Name'] = Header(tracker_name, charset)
+
         # and another one to avoid loops
-        writer.addheader('X-Roundup-Loop', 'hello')
+        message['X-Roundup-Loop'] = 'hello'
         # finally, an aid to debugging problems
-        writer.addheader('X-Roundup-Version', __version__)
+        message['X-Roundup-Version'] = __version__
 
-        writer.addheader('MIME-Version', '1.0')       
-        
-        return message, writer
+        return message
 
     def standard_message(self, to, subject, content, author=None):
         """Send a standard message.
@@ -76,15 +120,13 @@ class Mailer:
         - subject: the subject as a string.
         - content: the body of the message as a string.
         - author: the sender as a (name, address) tuple
-        """
-        message, writer = self.get_standard_message(to, subject, author)
-
-        writer.addheader('Content-Transfer-Encoding', 'quoted-printable')
-        body = writer.startbody('text/plain; charset=utf-8')
-        content = StringIO(content)
-        quopri.encode(content, body, 0)
 
-        self.smtp_send(to, message)
+        All strings are assumed to be UTF-8 encoded.
+        """
+        message = self.get_standard_message(to, subject, author)
+        message.set_payload(content)
+        encode_quopri(message)
+        self.smtp_send(to, message.as_string())
 
     def bounce_message(self, bounced_message, to, error,
                        subject='Failed issue tracker submission'):
@@ -92,61 +134,86 @@ class Mailer:
 
         Arguments:
         - bounced_message: an RFC822 Message object.
-        - to: a list of addresses usable by rfc822.parseaddr().
+        - to: a list of addresses usable by rfc822.parseaddr(). Might be
+          extended or overridden according to the config
+          ERROR_MESSAGES_TO setting.
         - error: the reason of failure as a string.
         - subject: the subject as a string.
-        
+
         """
-        message, writer = self.get_standard_message(to, subject)
+        # see whether we should send to the dispatcher or not
+        dispatcher_email = getattr(self.config, "DISPATCHER_EMAIL",
+            getattr(self.config, "ADMIN_EMAIL"))
+        error_messages_to = getattr(self.config, "ERROR_MESSAGES_TO", "user")
+        if error_messages_to == "dispatcher":
+            to = [dispatcher_email]
+        elif error_messages_to == "both":
+            to.append(dispatcher_email)
 
-        part = writer.startmultipartbody('mixed')
-        part = writer.nextpart()
-        part.addheader('Content-Transfer-Encoding', 'quoted-printable')
-        body = part.startbody('text/plain; charset=utf-8')
-        body.write('\n'.join(error))
+        message = self.get_standard_message(to, subject, multipart=True)
 
-        # attach the original message to the returned message
-        part = writer.nextpart()
-        part.addheader('Content-Disposition', 'attachment')
-        part.addheader('Content-Description', 'Message you sent')
-        body = part.startbody('text/plain')
+        # add the error text
+        part = MIMEText('\n'.join(error))
+        message.attach(part)
 
+        # attach the original message to the returned message
+        body = []
         for header in bounced_message.headers:
-            body.write(header)
-        body.write('\n')
+            body.append(header)
         try:
             bounced_message.rewindbody()
-        except IOError, message:
-            body.write("*** couldn't include message body: %s ***"
-                       % bounced_message)
+        except IOError, errmessage:
+            body.append("*** couldn't include message body: %s ***" %
+                errmessage)
         else:
-            body.write(bounced_message.fp.read())
+            body.append('\n')
+            body.append(bounced_message.fp.read())
+        part = MIMEText(''.join(body))
+        message.attach(part)
 
-        writer.lastpart()
+        # send
+        try:
+            self.smtp_send(to, message.as_string())
+        except MessageSendError:
+            # squash mail sending errors when bouncing mail
+            # TODO this *could* be better, as we could notify admin of the
+            # problem (even though the vast majority of bounce errors are
+            # because of spam)
+            pass
+
+    def exception_message(self):
+        '''Send a message to the admins with information about the latest
+        traceback.
+        '''
+        subject = '%s: %s'%(self.config.TRACKER_NAME, sys.exc_info()[1])
+        to = [self.config.ADMIN_EMAIL]
+        content = '\n'.join(traceback.format_exception(*sys.exc_info()))
+        self.standard_message(to, subject, content)
 
-        self.smtp_send(to, message)
-        
-    def smtp_send(self, to, message):
+    def smtp_send(self, to, message, sender=None):
         """Send a message over SMTP, using roundup's config.
 
         Arguments:
         - to: a list of addresses usable by rfc822.parseaddr().
         - message: a StringIO instance with a full message.
+        - sender: if not 'None', the email address to use as the
+        envelope sender.  If 'None', the admin email is used.
         """
+
+        if not sender:
+            sender = self.config.ADMIN_EMAIL
         if self.debug:
             # don't send - just write to a file
             open(self.debug, 'a').write('FROM: %s\nTO: %s\n%s\n' %
-                                        (self.config.ADMIN_EMAIL,
-                                         ', '.join(to),
-                                         message.getvalue()))
+                                        (sender,
+                                         ', '.join(to), message))
         else:
             # now try to send the message
             try:
                 # send the message as admin so bounces are sent there
                 # instead of to roundup
                 smtp = SMTPConnection(self.config)
-                smtp.sendmail(self.config.ADMIN_EMAIL, to,
-                              message.getvalue())
+                smtp.sendmail(sender, to, message)
             except socket.error, value:
                 raise MessageSendError("Error: couldn't send email: "
                                        "mailhost %s"%value)
@@ -157,42 +224,18 @@ class SMTPConnection(smtplib.SMTP):
     ''' Open an SMTP connection to the mailhost specified in the config
     '''
     def __init__(self, config):
-        
-        smtplib.SMTP.__init__(self, config.MAILHOST)
-
-        # use TLS?
-        use_tls = getattr(config, 'MAILHOST_TLS', 'no')
-        if use_tls == 'yes':
-            # do we have key files too?
-            keyfile = getattr(config, 'MAILHOST_TLS_KEYFILE', '')
-            if keyfile:
-                certfile = getattr(config, 'MAILHOST_TLS_CERTFILE', '')
-                if certfile:
-                    args = (keyfile, certfile)
-                else:
-                    args = (keyfile, )
-            else:
-                args = ()
-            # start the TLS
-            self.starttls(*args)
+        smtplib.SMTP.__init__(self, config.MAILHOST, port=config['MAIL_PORT'],
+                              local_hostname=config['MAIL_LOCAL_HOSTNAME'])
+
+        # start the TLS if requested
+        if config["MAIL_TLS"]:
+            self.ehlo()
+            self.starttls(config["MAIL_TLS_KEYFILE"],
+                config["MAIL_TLS_CERTFILE"])
 
         # ok, now do we also need to log in?
-        mailuser = getattr(config, 'MAILUSER', None)
+        mailuser = config["MAIL_USERNAME"]
         if mailuser:
-            self.login(*config.MAILUSER)
-
-# use the 'email' module, either imported, or our copied version
-try :
-    from email.Utils import formataddr as straddr
-except ImportError :
-    # code taken from the email package 2.4.3
-    def straddr(pair, specialsre = re.compile(r'[][\()<>@,:;".]'),
-            escapesre = re.compile(r'[][\()"]')):
-        name, address = pair
-        if name:
-            quotes = ''
-            if specialsre.search(name):
-                quotes = '"'
-            name = escapesre.sub(r'\\\g<0>', name)
-            return '%s%s%s <%s>' % (quotes, name, quotes, address)
-        return address
+            self.login(mailuser, config["MAIL_PASSWORD"])
+
+# vim: set et sts=4 sw=4 :