Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: start to reconcile internal inconsistencies wrt multiple from values #7935

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading