From 8e37f6068604dac2f25885db92143b6d0711b3a7 Mon Sep 17 00:00:00 2001 From: Hadley Date: Wed, 22 Feb 2017 19:22:54 -0800 Subject: [PATCH] Make generated message IDs for emails less likely to upset spam filters --- src/calibre/utils/smtp.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/calibre/utils/smtp.py b/src/calibre/utils/smtp.py index e6ad2e9363..6c7da2f3b2 100644 --- a/src/calibre/utils/smtp.py +++ b/src/calibre/utils/smtp.py @@ -18,17 +18,24 @@ 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, make_msgid + from email.utils import formatdate from email import encoders + import uuid outer = MIMEMultipart() outer['Subject'] = subject outer['To'] = to outer['From'] = from_ outer['Date'] = formatdate(localtime=True) - outer['Message-Id'] = make_msgid() 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] + if not msgid_domain: + # from address didn't provide a domain, let's make a best guess + msgid_domain = safe_localhost() + outer['Message-Id'] = "<{0}@{1}>".format(uuid.uuid4(), msgid_domain) + if text is not None: from email.mime.text import MIMEText if isbytestring(text):