Added send_pgp_mime.Mail.encodedMIMEText() for unicode handling.
authorW. Trevor King <wking@drexel.edu>
Sat, 18 Jul 2009 12:47:11 +0000 (08:47 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 20 Mar 2012 21:18:45 +0000 (17:18 -0400)
Now be-handle-mail handles examples/unicode without crashing
  cat examples/unicode | ./be-handle-mail -o -l -
But the output email is encoded in base64:

MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
From: BE Bugs <wking@thor.physics.drexel.edu>
To: John Doe <jdoe@example.com>
Date: Sat, 18 Jul 2009 12:22:05 +0000
Subject: [be-bug] Re: show
In-reply-to: <abcd@example.com>

UmVzdWx0cyBvZiBydW5uaW5nOiAoZXhpdCBjb2RlIDApCiAgc2hvdyAKCnN0ZG91dDoKCjw/eG1s
IHZlcnNpb249IjEuMCIgZW5jb2Rpbmc9IlVURi04IiA/Pgo8YnVnPgogIDx1dWlkPmY3Y2NkOTE2
LWI1YzctNDg5MC1hMmUzLThjOGFjZTE3YWUzYTwvdXVpZD4KICA8c2hvcnQtbmFtZT5mN2M8L3No
b3J0LW5hbWU+CiAgPHNldmVyaXR5Pm1pbm9yPC9zZXZlcml0eT4KICA8c3RhdHVzPmZpeGVkPC9z
...

This is perhaps the best we can get out of python < 3.1/2.7, see
  http://bugs.python.org/issue1368247

interfaces/email/interactive/send_pgp_mime.py

index a10674acdc69689fd06ba064b7401816f6be3799..f66c6263c85cc89a823134a2df1576d78d547215 100644 (file)
@@ -80,6 +80,7 @@ have been warned.
 verboseInvoke = False
 PGP_SIGN_AS = None
 PASSPHRASE = None
+DEFAULT_BODY_ENCODING = "UTF-8"
 
 # The following commands are adapted from my .mutt/pgp configuration
 # 
@@ -164,7 +165,8 @@ def flatten(msg):
     g = Generator(fp, mangle_from_=False)
     g.flatten(msg)
     text = fp.getvalue()
-    return text    
+    encoding = msg.get_content_charset()
+    return unicode(text, encoding=encoding)
 
 def source_email(msg, return_realname=False):
     """
@@ -342,8 +344,15 @@ class Mail (object):
         return source_email(self.headermsg)
     def targetEmails(self):
         return target_emails(self.headermsg)
+    def encodedMIMEText(self, body, encoding=None):
+        if encoding == None:
+            encoding = DEFAULT_BODY_ENCODING
+        if type(body) == types.StringType:
+            encoding = "US-ASCII"
+        # Create the message ('plain' stands for Content-Type: text/plain)
+        return MIMEText(body.encode(encoding), 'plain', encoding)
     def clearBodyPart(self):
-        body = MIMEText(self.body)
+        body = self.encodedMIMEText(self.body)
         body.add_header('Content-Disposition', 'inline')
         return body
     def passphrase_arg(self, passphrase=None):
@@ -356,7 +365,7 @@ class Mail (object):
         """
         text/plain
         """        
-        msg = MIMEText(self.body)
+        msg = self.encodedMIMEText(self.body)
         for k,v in self.headermsg.items():
             msg[k] = v
         return msg