From 5846d4c1ad7dbc7651613f1d1b2c619ac3378b76 Mon Sep 17 00:00:00 2001 From: Hadley Date: Wed, 22 Feb 2017 14:30:59 -0800 Subject: [PATCH 1/3] Add Message-Id header to outgoing emails. --- src/calibre/utils/smtp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/calibre/utils/smtp.py b/src/calibre/utils/smtp.py index ab7c93d189..e6ad2e9363 100644 --- a/src/calibre/utils/smtp.py +++ b/src/calibre/utils/smtp.py @@ -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, make_msgid from email import encoders outer = MIMEMultipart() @@ -26,6 +26,7 @@ def create_mail(from_, to, subject, text=None, attachment_data=None, 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' if text is not None: From 8e37f6068604dac2f25885db92143b6d0711b3a7 Mon Sep 17 00:00:00 2001 From: Hadley Date: Wed, 22 Feb 2017 19:22:54 -0800 Subject: [PATCH 2/3] 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): From 865733fc73b5886cdffa76c10eb3b72503237f31 Mon Sep 17 00:00:00 2001 From: Hadley Date: Wed, 22 Feb 2017 20:01:28 -0800 Subject: [PATCH 3/3] More robust extraction of domain from email address when generating message IDs. --- src/calibre/utils/smtp.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/calibre/utils/smtp.py b/src/calibre/utils/smtp.py index 6c7da2f3b2..a70458e8c6 100644 --- a/src/calibre/utils/smtp.py +++ b/src/calibre/utils/smtp.py @@ -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()