Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

email.py: added message id and local_hostname to get better formed me… #172

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export COPANIER_SMTP_HOST="mail.gandi.net"
export COPANIER_SMTP_PASSWORD="something"
export COPANIER_SMTP_LOGIN="yourlogin"
export COPANIER_FROM_EMAIL="[email protected]"
export COPANIER_DOMAIN="tld.com"
export COPANIER_EMAIL_SIGNATURE="The team"
export COPANIER_STAFF="[email protected] [email protected]"
```
Expand Down
1 change: 1 addition & 0 deletions copanier/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SMTP_PASSWORD = ""
SMTP_LOGIN = ""
FROM_EMAIL = ""
DOMAIN = ""
STAFF = []
HIDE_GROUPS_LINK = False
LOCALE = "fr_FR.UTF-8"
Expand Down
14 changes: 13 additions & 1 deletion copanier/emails.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from emails import Message
import email.utils as utils

from . import config

Expand All @@ -7,8 +8,10 @@ def send(to, subject, body, html=None, copy=None, attachments=None, mail_from=No
if not attachments:
attachments = []

# compute a message id, this is good for spam score
mid = utils.make_msgid(domain=config.FROM_EMAIL.partition('@')[2])
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:
Expand All @@ -18,12 +21,20 @@ def send(to, subject, body, html=None, copy=None, attachments=None, mail_from=No
body = body.replace("https", "http")
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 config.SMTP_LOGIN=="":
smtp={
"host": config.SMTP_HOST,
"port": "25",
"ssl": False,
"local_hostname": domain
}

else:
Expand All @@ -33,6 +44,7 @@ def send(to, subject, body, html=None, copy=None, attachments=None, mail_from=No
"password": config.SMTP_PASSWORD,
"port": "25",
"ssl": False,
"local_hostname": domain
}

message.send(
Expand Down