feat: Add Additional SMTP Headers to Decrease Spam Score (#3031)

* add html2text util

* add various missing email headers

* lint
This commit is contained in:
Michael Genson 2024-01-22 13:40:09 -06:00 committed by GitHub
parent 600c569ae8
commit 702907fc30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 2 deletions

View File

@ -3,6 +3,10 @@ import typing
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
from email import message from email import message
from email.utils import formatdate
from uuid import uuid4
from html2text import html2text
from mealie.services._base_service import BaseService from mealie.services._base_service import BaseService
@ -36,7 +40,19 @@ class Message:
msg["Subject"] = self.subject msg["Subject"] = self.subject
msg["From"] = f"{self.mail_from_name} <{self.mail_from_address}>" msg["From"] = f"{self.mail_from_name} <{self.mail_from_address}>"
msg["To"] = to msg["To"] = to
msg["Date"] = formatdate(localtime=True)
msg.add_alternative(self.html, subtype="html") msg.add_alternative(self.html, subtype="html")
msg.add_alternative(html2text(self.html), subtype="plain")
try:
message_id = f"{uuid4()}@{self.mail_from_address.split('@')[1]}"
except IndexError:
# this should never happen with a valid email address,
# but we let the SMTP server handle it instead of raising it here
message_id = f"{uuid4()}@{self.mail_from_address}"
msg["Message-ID"] = message_id
msg["MIME-Version"] = "1.0"
if smtp.ssl: if smtp.ssl:
with smtplib.SMTP_SSL(smtp.host, smtp.port) as server: with smtplib.SMTP_SSL(smtp.host, smtp.port) as server:

14
poetry.lock generated
View File

@ -708,6 +708,17 @@ files = [
[package.dependencies] [package.dependencies]
lxml = "*" lxml = "*"
[[package]]
name = "html2text"
version = "2020.1.16"
description = "Turn HTML into equivalent Markdown-structured text."
optional = false
python-versions = ">=3.5"
files = [
{file = "html2text-2020.1.16-py3-none-any.whl", hash = "sha256:c7c629882da0cf377d66f073329ccf34a12ed2adf0169b9285ae4e63ef54c82b"},
{file = "html2text-2020.1.16.tar.gz", hash = "sha256:e296318e16b059ddb97f7a8a1d6a5c1d7af4544049a01e261731d2d5cc277bbb"},
]
[[package]] [[package]]
name = "html5lib" name = "html5lib"
version = "1.1" version = "1.1"
@ -2013,7 +2024,6 @@ files = [
{file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"},
{file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"},
{file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"},
{file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"},
{file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"},
{file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"},
{file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"},
@ -2938,4 +2948,4 @@ pgsql = ["psycopg2-binary"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "3af89a4b335d71028761cbd6c2293eda5dbd0d0518d1dacd60440ef5401ba873" content-hash = "80b15507db71f3e7f66c8391405aa2a47f901fb94296006495ed47027ee8d88d"

View File

@ -44,6 +44,7 @@ beautifulsoup4 = "^4.11.2"
isodate = "^0.6.1" isodate = "^0.6.1"
text-unidecode = "^1.3" text-unidecode = "^1.3"
rapidfuzz = "^3.2.0" rapidfuzz = "^3.2.0"
html2text = "^2020.1.16"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
black = "^23.7.0" black = "^23.7.0"