send_pgp_mime.py attempts to avoid UTF-8 for MIMEText messages.
authorW. Trevor King <wking@drexel.edu>
Sat, 18 Jul 2009 13:04:25 +0000 (09:04 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 20 Mar 2012 21:19:05 +0000 (17:19 -0400)
This keeps the transfer-encoding out of base64 if possible.

Also added a "help" example to interafaces/email/interactive/examples.

interfaces/email/interactive/send_pgp_mime.py

index f66c6263c85cc89a823134a2df1576d78d547215..38a2437b0c7d5eb421334f85aeef6acf2ebbe433 100644 (file)
@@ -80,7 +80,6 @@ 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
 # 
@@ -346,11 +345,22 @@ class Mail (object):
         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"
+            if type(body) == types.StringType:
+                encoding = "US-ASCII"
+            elif type(body) == types.UnicodeType:
+                for encoding in ["US-ASCII", "ISO-8859-1", "UTF-8"]:
+                    try:
+                        body.encode(encoding)
+                    except UnicodeError:
+                        pass
+                    else:
+                        break
+                assert encoding != None
         # Create the message ('plain' stands for Content-Type: text/plain)
-        return MIMEText(body.encode(encoding), 'plain', encoding)
+        if encoding == "US-ASCII":
+            return MIMEText(body)
+        else:
+            return MIMEText(body.encode(encoding), 'plain', encoding)            
     def clearBodyPart(self):
         body = self.encodedMIMEText(self.body)
         body.add_header('Content-Disposition', 'inline')