Possible fix for #2301 (sending emails from custom domain name server)

This commit is contained in:
Ozzie Isaacs 2022-02-14 17:35:20 +01:00
parent e9b674f46e
commit ef7c6731bc

26
cps/tasks/mail.py Normal file → Executable file
View File

@ -16,7 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import smtplib
import threading
@ -25,7 +24,7 @@ import mimetypes
from io import StringIO
from email.message import EmailMessage
from email.utils import parseaddr
from email import encoders
@ -37,6 +36,7 @@ from cps.services import gmail
from cps import logger, config
from cps import gdriveutils
import uuid
log = logger.create()
@ -122,15 +122,27 @@ class TaskEmail(CalibreTask):
self.asyncSMTP = None
self.results = dict()
# from calibre code:
# https://github.com/kovidgoyal/calibre/blob/731ccd92a99868de3e2738f65949f19768d9104c/src/calibre/utils/smtp.py#L60
def get_msgid_domain(self):
try:
# Parse out the address from the From line, and then the domain from that
from_email = parseaddr(self.settings["mail_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 'calibre-web.com'
def prepare_message(self):
message = EmailMessage()
# message = MIMEMultipart()
message['to'] = self.recipent
message['from'] = self.settings["mail_from"]
message['subject'] = self.subject
message['Message-Id'] = make_msgid('calibre-web')
message['From'] = self.settings["mail_from"]
message['To'] = self.recipent
message['Subject'] = self.subject
message['Date'] = formatdate(localtime=True)
# text = self.text
message['Message-Id'] = "{}@{}".format(uuid.uuid4(), self.get_msgid_domain()) # f"<{uuid.uuid4()}@{get_msgid_domain(from_)}>" # make_msgid('calibre-web')
message.set_content(self.text.encode('UTF-8'), "text", "plain")
if self.attachment:
data = self._get_attachment(self.filepath, self.attachment)