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

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