More robust extraction of domain from email address when generating message IDs.

This commit is contained in:
Hadley 2017-02-22 20:01:28 -08:00
parent 8e37f60686
commit 865733fc73

View File

@ -18,7 +18,7 @@ def create_mail(from_, to, subject, text=None, attachment_data=None,
assert text or attachment_data
from email.mime.multipart import MIMEMultipart
from email.utils import formatdate
from email.utils import formatdate, parseaddr
from email import encoders
import uuid
@ -30,7 +30,12 @@ def create_mail(from_, to, subject, text=None, attachment_data=None,
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
# generate a Message-Id for this email
msgid_domain = from_.partition("@")[2]
# Parse out the address from the From line, and then the domain from that
from_email = parseaddr(from_)[1]
msgid_domain = from_email.partition("@")[2].strip()
if msgid_domain.endswith(">"):
# This can sometimes sneak through parseaddr if the input is malformed
msgid_domain = msgid_domain[:-1]
if not msgid_domain:
# from address didn't provide a domain, let's make a best guess
msgid_domain = safe_localhost()