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

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