Blame SOURCES/BZ-1202680-handle-non-ascii-email.patch

d2a170
commit 4e1de20b61ae3227d9fc973193a60cf7997e8606
d2a170
Author: Michal Domonkos <mdomonko@redhat.com>
d2a170
Date:   Fri Feb 19 11:05:23 2016 +0100
d2a170
d2a170
    yum-cron: don't crash with non-ascii email. BZ 1202680
d2a170
    
d2a170
    Previously, we constructed our MIMEText object with the default us-ascii
d2a170
    charset, which caused it to encode the unicode string (self.output) with
d2a170
    the us-ascii codec.  This worked fine as long as the string contained
d2a170
    ascii-only chars.  However, if yum-cron was run with a language which
d2a170
    makes use of non-ascii chars, this would fail and MIMEText would crash.
d2a170
    
d2a170
    To fix that, we need to tell MIMEText to encode the message with utf-8
d2a170
    instead.  However, that also causes the message to be transfer-encoded
d2a170
    to base64 which is heavier and uglier, so let's limit that to non-ascii
d2a170
    email only.
d2a170
d2a170
diff --git a/yum-cron/yum-cron.py b/yum-cron/yum-cron.py
d2a170
index 039f537..ccba690 100755
d2a170
--- a/yum-cron/yum-cron.py
d2a170
+++ b/yum-cron/yum-cron.py
d2a170
@@ -223,8 +223,18 @@ class EmailEmitter(UpdateEmitter):
d2a170
         # Don't send empty emails
d2a170
         if not self.output:
d2a170
             return
d2a170
-        # Build up the email to be sent
d2a170
-        msg = MIMEText(''.join(self.output))
d2a170
+        # Build up the email to be sent.  Encode it with us-ascii instead of
d2a170
+        # utf-8 if possible.  This ensures the email package will not
d2a170
+        # transfer-encode it to base64 in such a case (it decides based on the
d2a170
+        # charset passed to the MIMEText constructor).
d2a170
+        output = ''.join(self.output)
d2a170
+        try:
d2a170
+            output.encode('us-ascii')
d2a170
+        except UnicodeEncodeError:
d2a170
+            charset = 'utf-8'
d2a170
+        else:
d2a170
+            charset = 'us-ascii'
d2a170
+        msg = MIMEText(output, 'plain', charset)
d2a170
         msg['Subject'] = self.subject
d2a170
         msg['From'] = self.opts.email_from
d2a170
         msg['To'] = ",".join(self.opts.email_to)