email.py: added message id and local_hostname to get better formed messages, far from spams

This commit is contained in:
Emmanuel Courcelle & Projet Internet et Citoyenneté 2023-10-18 19:21:50 +02:00
parent 0485ff4305
commit 2f07929eef
3 changed files with 15 additions and 1 deletions

View file

@ -168,6 +168,7 @@ export COPANIER_SMTP_HOST="mail.gandi.net"
export COPANIER_SMTP_PASSWORD="something" export COPANIER_SMTP_PASSWORD="something"
export COPANIER_SMTP_LOGIN="yourlogin" export COPANIER_SMTP_LOGIN="yourlogin"
export COPANIER_FROM_EMAIL="youremail@tld.com" export COPANIER_FROM_EMAIL="youremail@tld.com"
export COPANIER_DOMAIN="tld.com"
export COPANIER_EMAIL_SIGNATURE="The team" export COPANIER_EMAIL_SIGNATURE="The team"
export COPANIER_STAFF="staff@email.com another@staff.com" export COPANIER_STAFF="staff@email.com another@staff.com"
``` ```

View file

@ -11,6 +11,7 @@ SMTP_HOST = ""
SMTP_PASSWORD = "" SMTP_PASSWORD = ""
SMTP_LOGIN = "" SMTP_LOGIN = ""
FROM_EMAIL = "" FROM_EMAIL = ""
DOMAIN = ""
STAFF = [] STAFF = []
HIDE_GROUPS_LINK = False HIDE_GROUPS_LINK = False
LOCALE = "fr_FR.UTF-8" LOCALE = "fr_FR.UTF-8"

View file

@ -1,4 +1,5 @@
from emails import Message from emails import Message
import email.utils as utils
from . import config from . import config
@ -7,8 +8,10 @@ def send(to, subject, body, html=None, copy=None, attachments=None, mail_from=No
if not attachments: if not attachments:
attachments = [] attachments = []
# compute a message id, this is good for spam score
mid = utils.make_msgid(domain=config.FROM_EMAIL.partition('@')[2])
message = Message( message = Message(
text=body, html=html, subject=subject, mail_from=config.FROM_EMAIL text=body, html=html, subject=subject, mail_from=config.FROM_EMAIL, message_id=mid
) )
for filename, attachment, mime in attachments: for filename, attachment, mime in attachments:
@ -18,12 +21,20 @@ def send(to, subject, body, html=None, copy=None, attachments=None, mail_from=No
body = body.replace("https", "http") body = body.replace("https", "http")
return print("Sending email", str(body.encode('utf-8')), flush=True) return print("Sending email", str(body.encode('utf-8')), flush=True)
# If the DOMAIN configuration parameter is configured, take it as HELO parameter
# Else, take None, the sender's fqdn will be computed by the library
# cf. https://docs.python.org/3/library/smtplib.html
domain = config.DOMAIN
if domain == "":
domain = None
# if no SMTP_LOGIN specified, don't create user and password fields, as the smtp server don't want them ! # if no SMTP_LOGIN specified, don't create user and password fields, as the smtp server don't want them !
if config.SMTP_LOGIN=="": if config.SMTP_LOGIN=="":
smtp={ smtp={
"host": config.SMTP_HOST, "host": config.SMTP_HOST,
"port": "25", "port": "25",
"ssl": False, "ssl": False,
"local_hostname": domain
} }
else: else:
@ -33,6 +44,7 @@ def send(to, subject, body, html=None, copy=None, attachments=None, mail_from=No
"password": config.SMTP_PASSWORD, "password": config.SMTP_PASSWORD,
"port": "25", "port": "25",
"ssl": False, "ssl": False,
"local_hostname": domain
} }
message.send( message.send(