Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_extract_sender_valueerror_on_multiple_resent_message():
message = EmailMessage()
message["Resent-Date"] = "Mon, 20 Nov 2016 21:04:27 -0000"
message["Resent-Date"] = "Mon, 20 Nov 2017 21:04:27 -0000"
with pytest.raises(ValueError):
extract_sender(message)
def test_extract_sender(mime_header, compat32_header, expected_sender):
mime_message = EmailMessage()
mime_message["From"] = mime_header
mime_sender = extract_sender(mime_message)
assert mime_sender == expected_sender
compat32_message = Message()
compat32_message["From"] = compat32_header
compat32_sender = extract_sender(compat32_message)
assert compat32_sender == expected_sender
def test_extract_sender(mime_header, compat32_header, expected_sender):
mime_message = EmailMessage()
mime_message["From"] = mime_header
mime_sender = extract_sender(mime_message)
assert mime_sender == expected_sender
compat32_message = Message()
compat32_message["From"] = compat32_header
compat32_sender = extract_sender(compat32_message)
assert compat32_sender == expected_sender
def test_extract_sender_resent_message():
message = EmailMessage()
message["From"] = "alice@example.com"
message["Resent-Date"] = "Mon, 20 Nov 2017 21:04:27 -0000"
message["Resent-From"] = "hubert@example.com"
sender = extract_sender(message)
assert sender == message["Resent-From"]
assert sender != message["From"]
def test_extract_sender_prefers_sender_header():
message = EmailMessage()
message["From"] = "bob@example.com"
message["Sender"] = "alice@example.com"
sender = extract_sender(message)
assert sender != message["From"]
assert sender == message["Sender"]
so rather than guess we raise a ``ValueError`` in that case.
:raises ValueError:
on more than one Resent header block
on no sender kwarg or From header in message
on no recipients kwarg or To, Cc or Bcc header in message
:raises SMTPRecipientsRefused: delivery to all recipients failed
:raises SMTPResponseException: on invalid response
"""
if mail_options is None:
mail_options = []
else:
mail_options = list(mail_options)
if sender is None:
sender = extract_sender(message)
if not sender:
raise ValueError("No From header provided in message")
if isinstance(recipients, str):
recipients = [recipients]
elif recipients is None:
recipients = extract_recipients(message)
if not recipients:
raise ValueError("No recipient headers provided in message")
# Make sure we've done an EHLO for extension checks
await self._ehlo_or_helo_if_needed()
try:
sender.encode("ascii")
"".join(recipients).encode("ascii")