Skip to content

Commit

Permalink
fix: start to reconcile internal inconsistencies wrt multiple from va…
Browse files Browse the repository at this point in the history
…lues (#7935)
  • Loading branch information
rjsparks committed Sep 16, 2024
1 parent f5c132a commit 3b5058a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 13 additions & 1 deletion ietf/utils/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ def send_smtp(msg, bcc=None):
'''
mark = time.time()
add_headers(msg)
(fname, frm) = parseaddr(msg.get('From'))
# N.B. We have a disconnect with most of this code assuming a From header value will only
# have one address.
# The frm computed here is only used as the envelope from.
# Previous code simply ran `parseaddr(msg.get('From'))`, getting lucky if the string returned
# from the get had more than one address in it. Python 3.9.20 changes the behavior of parseaddr
# and that erroneous use of the function no longer gets lucky.
# For the short term, to match behavior to date as closely as possible, if we get a message
# that has multiple addresses in the From header, we will use the first for the envelope from
from_tuples = getaddresses(msg.get_all('From', [settings.DEFAULT_FROM_EMAIL]))
assertion('len(from_tuples)==1', note=f"send_smtp received multiple From addresses: {from_tuples}")
_ , frm = from_tuples[0]
addrlist = msg.get_all('To') + msg.get_all('Cc', [])
if bcc:
addrlist += [bcc]
Expand Down Expand Up @@ -446,6 +456,8 @@ def parse_preformatted(preformatted, extra=None, override=None):
values = msg.get_all(key, [])
if values:
values = getaddresses(values)
if key=='From':
assertion('len(values)<2', note=f'parse_preformatted is constructing a From with multiple values: {values}')
del msg[key]
msg[key] = ',\n '.join(formataddr(v) for v in values)
for key in ['Subject', ]:
Expand Down
12 changes: 6 additions & 6 deletions ietf/utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SendingMail(TestCase):

def test_send_mail_preformatted(self):
msg = """To: [email protected], [email protected]
From: [email protected], [email protected]
From: [email protected]
Cc: [email protected], [email protected]
Bcc: [email protected], [email protected]
Subject: subject
Expand All @@ -63,37 +63,37 @@ def test_send_mail_preformatted(self):
send_mail_preformatted(None, msg, {}, {})
recv = outbox[-1]
self.assertSameEmail(recv['To'], '<[email protected]>, <[email protected]>')
self.assertSameEmail(recv['From'], '[email protected], [email protected]')
self.assertSameEmail(recv['From'], '[email protected]')
self.assertSameEmail(recv['Cc'], '[email protected], [email protected]')
self.assertSameEmail(recv['Bcc'], None)
self.assertEqual(recv['Subject'], 'subject')
self.assertEqual(get_payload_text(recv), 'body\n')

override = {
'To': '[email protected], [email protected]',
'From': '[email protected], [email protected]',
'From': '[email protected]',
'Cc': '[email protected], [email protected]',
'Subject': 'osubject',
}
send_mail_preformatted(request=None, preformatted=msg, extra={}, override=override)
recv = outbox[-1]
self.assertSameEmail(recv['To'], '<[email protected]>, <[email protected]>')
self.assertSameEmail(recv['From'], '[email protected], [email protected]')
self.assertSameEmail(recv['From'], '[email protected]')
self.assertSameEmail(recv['Cc'], '[email protected], [email protected]')
self.assertSameEmail(recv['Bcc'], None)
self.assertEqual(recv['Subject'], 'osubject')
self.assertEqual(recv.get_payload(), 'body\n')

override = {
'To': ['<[email protected]>', '[email protected]'],
'From': ['<[email protected]>', '[email protected]'],
'From': ['<[email protected]>'],
'Cc': ['<[email protected]>', '[email protected]'],
'Subject': 'osubject',
}
send_mail_preformatted(request=None, preformatted=msg, extra={}, override=override)
recv = outbox[-1]
self.assertSameEmail(recv['To'], '<[email protected]>, <[email protected]>')
self.assertSameEmail(recv['From'], '<[email protected]>, [email protected]')
self.assertSameEmail(recv['From'], '<[email protected]>')
self.assertSameEmail(recv['Cc'], '<[email protected]>, [email protected]')
self.assertSameEmail(recv['Bcc'], None)
self.assertEqual(recv['Subject'], 'osubject')
Expand Down

0 comments on commit 3b5058a

Please sign in to comment.