From 4988d9378c282b80139b24b05cbc42e4a217db44 Mon Sep 17 00:00:00 2001 From: Robert Sparks Date: Tue, 3 Feb 2015 20:18:27 +0000 Subject: [PATCH] Avoid an issue with python.email breaking To header field values that it has to encode in bad places. Fixes #1589. Commit ready for merge. - Legacy-Id: 8961 --- ietf/utils/mail.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ietf/utils/mail.py b/ietf/utils/mail.py index d08cee82a..dcc8173a4 100644 --- a/ietf/utils/mail.py +++ b/ietf/utils/mail.py @@ -4,6 +4,7 @@ from email.Utils import make_msgid, formatdate, formataddr, parseaddr, getaddres from email.MIMEText import MIMEText from email.MIMEMessage import MIMEMessage from email.MIMEMultipart import MIMEMultipart +from email.header import Header from email import message_from_string import smtplib from django.conf import settings @@ -190,7 +191,25 @@ def condition_message(to, frm, subject, msg, cc, extra): cc = ", ".join([isinstance(addr, tuple) and formataddr(addr) or addr for addr in cc if addr]) if frm: msg['From'] = frm - msg['To'] = to + + # The following is a hack to avoid an issue with how the email module (as of version 4.0.3) + # breaks lines when encoding header fields with anything other than the us-ascii codec. + # This allows the Header implementation to encode each display name as a separate chunk. + # The resulting encode produces a string that is us-ascii and has a good density of + # "higher-level syntactic breaks" + to_hdr = Header(header_name='To') + for name, addr in getaddresses([to]): + if addr != '' and not addr.startswith('unknown-email-'): + if name: + to_hdr.append('"%s"' % name) + to_hdr.append("<%s>," % addr) + to_str = to_hdr.encode() + if to_str and to_str[-1] == ',': + to_str=to_str[:-1] + # It's important to use this string, and not assign the Header object. + # Code downstream from this assumes that the msg['To'] will return a string, not an instance + msg['To'] = to_str + if cc: msg['Cc'] = cc msg['Subject'] = subject