#16 add Date header to email

This commit is contained in:
Davide Alberani 2021-10-12 18:16:35 +02:00
parent 3563638dab
commit fbd2988936

View file

@ -23,6 +23,7 @@ import shutil
import urllib import urllib
import smtplib import smtplib
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.utils import formatdate
import logging import logging
import datetime import datetime
import requests import requests
@ -293,6 +294,7 @@ def send_email(to, subject='diffido', body='', from_=None):
msg['Subject'] = subject msg['Subject'] = subject
msg['From'] = from_ or EMAIL_FROM msg['From'] = from_ or EMAIL_FROM
msg['To'] = to msg['To'] = to
msg["Date"] = formatdate(localtime=True)
starttls = SMTP_SETTINGS.get('smtp-starttls') starttls = SMTP_SETTINGS.get('smtp-starttls')
use_ssl = SMTP_SETTINGS.get('smtp-use-ssl') use_ssl = SMTP_SETTINGS.get('smtp-use-ssl')
username = SMTP_SETTINGS.get('smtp-username') username = SMTP_SETTINGS.get('smtp-username')
@ -307,8 +309,10 @@ def send_email(to, subject='diffido', body='', from_=None):
args[key] = value args[key] = value
try: try:
if use_ssl: if use_ssl:
logger.debug('STMP SSL connection with args: %s' % repr(args))
with smtplib.SMTP_SSL(**args) as s: with smtplib.SMTP_SSL(**args) as s:
if username: if username:
logger.debug('STMP LOGIN for username %s and password of length %d' % (username, len(password)))
s.login(username, password) s.login(username, password)
s.send_message(msg) s.send_message(msg)
else: else:
@ -317,11 +321,14 @@ def send_email(to, subject='diffido', body='', from_=None):
if key in args: if key in args:
tls_args[key.replace('ssl_', '')] = args[key] tls_args[key.replace('ssl_', '')] = args[key]
del args[key] del args[key]
logger.debug('STMP connection with args: %s' % repr(args))
with smtplib.SMTP(**args) as s: with smtplib.SMTP(**args) as s:
if starttls: if starttls:
s.starttls(**tls_args) logger.debug('STMP STARTTLS connection with args: %s' % repr(tls_args))
s.ehlo_or_helo_if_needed() s.ehlo_or_helo_if_needed()
s.starttls(**tls_args)
if args.get('username'): if args.get('username'):
logger.debug('STMP LOGIN for username %s and password of length %d' % (username, len(password)))
s.login(username, password) s.login(username, password)
s.send_message(msg) s.send_message(msg)
except Exception as e: except Exception as e: