mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
E-mail delivery: Add a Message-ID header when sending emails. Reduces probability of mails sent from calibre being marked as spam
Merge branch 'master' of https://github.com/ThisIsADogHello/calibre
This commit is contained in:
commit
3ea07d58fb
@ -13,6 +13,19 @@ import sys, traceback, os, socket, encodings.idna as idna
|
|||||||
from calibre import isbytestring, force_unicode
|
from calibre import isbytestring, force_unicode
|
||||||
|
|
||||||
|
|
||||||
|
def get_msgid_domain(from_):
|
||||||
|
from email.utils import parseaddr
|
||||||
|
try:
|
||||||
|
# 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()
|
||||||
|
# This can sometimes sneak through parseaddr if the input is malformed
|
||||||
|
msgid_domain = msgid_domain.rstrip('>').strip()
|
||||||
|
except Exception:
|
||||||
|
msgid_domain = ''
|
||||||
|
return msgid_domain or safe_localhost()
|
||||||
|
|
||||||
|
|
||||||
def create_mail(from_, to, subject, text=None, attachment_data=None,
|
def create_mail(from_, to, subject, text=None, attachment_data=None,
|
||||||
attachment_type=None, attachment_name=None):
|
attachment_type=None, attachment_name=None):
|
||||||
assert text or attachment_data
|
assert text or attachment_data
|
||||||
@ -20,12 +33,14 @@ def create_mail(from_, to, subject, text=None, attachment_data=None,
|
|||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.utils import formatdate
|
from email.utils import formatdate
|
||||||
from email import encoders
|
from email import encoders
|
||||||
|
import uuid
|
||||||
|
|
||||||
outer = MIMEMultipart()
|
outer = MIMEMultipart()
|
||||||
outer['Subject'] = subject
|
outer['Subject'] = subject
|
||||||
outer['To'] = to
|
outer['To'] = to
|
||||||
outer['From'] = from_
|
outer['From'] = from_
|
||||||
outer['Date'] = formatdate(localtime=True)
|
outer['Date'] = formatdate(localtime=True)
|
||||||
|
outer['Message-Id'] = "<{}@{}>".format(uuid.uuid4(), get_msgid_domain())
|
||||||
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
|
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
|
||||||
|
|
||||||
if text is not None:
|
if text is not None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user