Skip to content

Commit bf77d1e

Browse files
committed
Merged in [8961] from rjsparks@nostrum.com:
Avoid an issue with python.email breaking To header field values that it has to encode in bad places. Fixes ietf-tools#1589. - Legacy-Id: 8983 Note: SVN reference [8961] has been migrated to Git commit 4988d93
2 parents 001ecb4 + 4988d93 commit bf77d1e

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

ietf/utils/mail.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from email.MIMEText import MIMEText
55
from email.MIMEMessage import MIMEMessage
66
from email.MIMEMultipart import MIMEMultipart
7+
from email.header import Header
78
from email import message_from_string
89
import smtplib
910
from django.conf import settings
@@ -190,7 +191,25 @@ def condition_message(to, frm, subject, msg, cc, extra):
190191
cc = ", ".join([isinstance(addr, tuple) and formataddr(addr) or addr for addr in cc if addr])
191192
if frm:
192193
msg['From'] = frm
193-
msg['To'] = to
194+
195+
# The following is a hack to avoid an issue with how the email module (as of version 4.0.3)
196+
# breaks lines when encoding header fields with anything other than the us-ascii codec.
197+
# This allows the Header implementation to encode each display name as a separate chunk.
198+
# The resulting encode produces a string that is us-ascii and has a good density of
199+
# "higher-level syntactic breaks"
200+
to_hdr = Header(header_name='To')
201+
for name, addr in getaddresses([to]):
202+
if addr != '' and not addr.startswith('unknown-email-'):
203+
if name:
204+
to_hdr.append('"%s"' % name)
205+
to_hdr.append("<%s>," % addr)
206+
to_str = to_hdr.encode()
207+
if to_str and to_str[-1] == ',':
208+
to_str=to_str[:-1]
209+
# It's important to use this string, and not assign the Header object.
210+
# Code downstream from this assumes that the msg['To'] will return a string, not an instance
211+
msg['To'] = to_str
212+
194213
if cc:
195214
msg['Cc'] = cc
196215
msg['Subject'] = subject

0 commit comments

Comments
 (0)