Code

email charset fixes
authorrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Sun, 29 Feb 2004 00:35:55 +0000 (00:35 +0000)
committerrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Sun, 29 Feb 2004 00:35:55 +0000 (00:35 +0000)
git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@2128 57a73879-2fb5-44c3-a270-3262357dd7e2

doc/upgrading.txt
roundup/mailer.py
roundup/roundupdb.py

index 3dcabed26c55c9636aeeddbe413dc8827943d8cd..2531ce867f9d2b3d10fa411267b9c84fefe7b24b 100644 (file)
@@ -31,6 +31,14 @@ Removed Database.curuserid attribute. Any code referencing this attribute
 should be replaced with a call to Database.getuid().
 
 
+0.7.0 Email character set
+-------------------------
+
+The default character set for sending email is UTF-8 (ie. Unicode). If you
+have users whose email clients can't handle UTF-8 (eg. Eudora) then you
+will need to edit the new config.py variable ``EMAIL_CHARSET``.
+
+
 0.7.0 ZRoundup changes
 ----------------------
 
index ea51fe5fb2af0fde6c62f358648c409bb784e233..93ef7f9ba5d26b3517014c86c42ec292f3a6bf3e 100644 (file)
@@ -1,7 +1,7 @@
 """Sending Roundup-specific mail over SMTP.
 """
 __docformat__ = 'restructuredtext'
-# $Id: mailer.py,v 1.6 2004-02-23 05:29:05 richard Exp $
+# $Id: mailer.py,v 1.7 2004-02-29 00:35:55 richard Exp $
 
 import time, quopri, os, socket, smtplib, re
 
@@ -24,20 +24,41 @@ class Mailer:
         self.debug = os.environ.get('SENDMAILDEBUG', '')
 
     def get_standard_message(self, to, subject, author=None):
+        '''Form a standard email message from Roundup.
+
+        "to"      - recipients list
+        "subject" - Subject
+        "author"  - (name, address) tuple or None for admin email
+
+        Subject and author are encoded using the EMAIL_CHARSET from the
+        config (default UTF-8).
+
+        Returns a Message object and body part writer.
+        '''
+        # 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)
         if not author:
-            author = straddr((self.config.TRACKER_NAME,
-                              self.config.ADMIN_EMAIL))
+            author = straddr((tracker_name, self.config.ADMIN_EMAIL))
+        else:
+            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,
-            self.config.EMAIL_CHARSET))
+        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()))
 
         # Add a unique Roundup header to help filtering
-        writer.addheader('X-Roundup-Name', self.config.TRACKER_NAME)
+        writer.addheader('X-Roundup-Name', encode_header(tracker_name,
+            charset))
         # and another one to avoid loops
         writer.addheader('X-Roundup-Loop', 'hello')
         # finally, an aid to debugging problems
@@ -54,7 +75,7 @@ class Mailer:
         - to: a list of addresses usable by rfc822.parseaddr().
         - subject: the subject as a string.
         - content: the body of the message as a string.
-        - author: the sender as a string, suitable for a 'From:' header.
+        - author: the sender as a (name, address) tuple
         """
         message, writer = self.get_standard_message(to, subject, author)
 
index 61608475c65804226275b79193dcf63f42ba943c..ddaf4dd2979b3ca4a3b0770a38213e600648a0d8 100644 (file)
@@ -15,7 +15,7 @@
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 # 
-# $Id: roundupdb.py,v 1.98 2004-02-23 05:29:05 richard Exp $
+# $Id: roundupdb.py,v 1.99 2004-02-29 00:35:55 richard Exp $
 
 """Extending hyperdb with types specific to issue-tracking.
 """
@@ -207,10 +207,11 @@ class IssueClass:
                                            self.db.config.MAIL_DOMAIN)
             messages.set(msgid, messageid=messageid)
 
-        # send an email to the people who missed out
+        # compose title
         cn = self.classname
         title = self.get(nodeid, 'title') or '%s message copy'%cn
 
+        # figure author information
         authid = messages.safeget(msgid, 'author')
         authname = users.safeget(authid, 'realname')
         if not authname:
@@ -248,7 +249,11 @@ class IssueClass:
             m.append(self.email_signature(nodeid, msgid))
 
         # encode the content as quoted-printable
-        content = cStringIO.StringIO('\n'.join(m))
+        charset = getattr(self.db.config, 'EMAIL_CHARSET', 'utf-8')
+        m = '\n'.join(m)
+        if charset != 'utf-8':
+            m = unicode(m, 'utf-8').encode(charset)
+        content = cStringIO.StringIO(m)
         content_encoded = cStringIO.StringIO()
         quopri.encode(content, content_encoded, 0)
         content_encoded = content_encoded.getvalue()
@@ -268,18 +273,21 @@ class IssueClass:
         if from_tag:
             from_tag = ' ' + from_tag
 
-        subject = '[%s%s] %s' % (cn, nodeid, encode_header(title,
-            self.db.config.EMAIL_CHARSET))
-        author = straddr((encode_header(authname, self.db.config.EMAIL_CHARSET)
-            + from_tag, from_address))
+        subject = '[%s%s] %s'%(cn, nodeid, title)
+        author = (authname + from_tag, from_address)
 
         # create the message
         mailer = Mailer(self.db.config)
         message, writer = mailer.get_standard_message(sendto, subject, author)
 
-        tracker_name = encode_header(self.db.config.TRACKER_NAME,
-            self.db.config.EMAIL_CHARSET)
+        # set reply-to to the tracker
+        tracker_name = self.db.config.TRACKER_NAME
+        if charset != 'utf-8':
+            tracker = unicode(tracker_name, 'utf-8').encode(charset)
+        tracker_name = encode_header(tracker_name, charset)
         writer.addheader('Reply-To', straddr((tracker_name, from_address)))
+
+        # message ids
         if messageid:
             writer.addheader('Message-Id', messageid)
         if inreplyto:
@@ -290,7 +298,7 @@ class IssueClass:
             part = writer.startmultipartbody('mixed')
             part = writer.nextpart()
             part.addheader('Content-Transfer-Encoding', 'quoted-printable')
-            body = part.startbody('text/plain; charset=utf-8')
+            body = part.startbody('text/plain; charset=%s'%charset)
             body.write(content_encoded)
             for fileid in message_files:
                 name = files.get(fileid, 'name')
@@ -318,7 +326,7 @@ class IssueClass:
             writer.lastpart()
         else:
             writer.addheader('Content-Transfer-Encoding', 'quoted-printable')
-            body = writer.startbody('text/plain; charset=utf-8')
+            body = writer.startbody('text/plain; charset=%s'%charset)
             body.write(content_encoded)
 
         mailer.smtp_send(sendto, message)