mail: mime-encode patches that are utf-8
authorChristian Ebert <blacktrash@gmx.net>
Mon, 20 Oct 2008 17:40:29 +0200
changeset 7191 d14212218582
parent 7183 099b4f9be5ab
child 7192 f31ba106fc19
mail: mime-encode patches that are utf-8 utf-8 can be safely detected without making assumptions on the encoding/locale used by the recipient. Content-Transfer-Encoding for utf-8 patches is base64 (default of python's email module).
mercurial/mail.py
--- a/mercurial/mail.py	Mon Oct 20 14:13:37 2008 +0200
+++ b/mercurial/mail.py	Mon Oct 20 17:40:29 2008 +0200
@@ -86,6 +86,17 @@
             raise util.Abort(_('%r specified as email transport, '
                                'but not in PATH') % method)
 
+def mimetextpatch(s, subtype='plain', display=False):
+    '''If patch in utf-8 transfer-encode it.'''
+    if not display:
+        for cs in ('us-ascii', 'utf-8'):
+            try:
+                s.decode(cs)
+                return email.MIMEText.MIMEText(s, subtype, cs)
+            except UnicodeDecodeError:
+                pass
+    return email.MIMEText.MIMEText(s, subtype)
+
 def _charsets(ui):
     '''Obtains charsets to send mail parts not containing patches.'''
     charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')]