fixes #16: support SMTP authentication

This commit is contained in:
Davide Alberani 2021-10-11 20:32:56 +02:00
parent 678a8bccc5
commit 2a88d5ab7a
2 changed files with 12 additions and 4 deletions

View file

@ -8,3 +8,5 @@ smtp_host='localhost'
#smtp_ssl_keyfile=None
#smtp_ssl_certfile=None
#smtp_ssl_context=None
#smtp_username=''
#smtp_password=''

View file

@ -306,17 +306,21 @@ def send_email(to, subject='diffido', body='', from_=None):
try:
if use_ssl:
with smtplib.SMTP_SSL(**args) as s:
if args.get('username'):
s.login(args.get('username', ''), args.get('password', ''))
s.send_message(msg)
else:
tls_args = {}
for key in ('ssl_keyfile', 'ssl_certfile', 'ssl_context'):
if key in args:
tls_args = args[key]
tls_args[key] = args[key].replace('ssl_', '')
del args[key]
with smtplib.SMTP(**args) as s:
if starttls:
s.starttls(**tls_args)
s.ehlo_or_helo_if_needed()
if args.get('username'):
s.login(args.get('username', ''), args.get('password', ''))
s.send_message(msg)
except Exception as e:
logger.error('unable to send email to %s: %s' % (to, e))
@ -713,9 +717,11 @@ def serve():
define('smtp-local-hostname', default=None, help='SMTP local hostname', type=str)
define('smtp-use-ssl', default=False, help='Use SSL to connect to the SMTP server', type=bool)
define('smtp-starttls', default=False, help='Use STARTTLS to connect to the SMTP server', type=bool)
define('smtp-ssl-keyfile', default=None, help='SSL key file', type=str)
define('smtp-ssl-certfile', default=None, help='SSL cert file', type=str)
define('smtp-ssl-context', default=None, help='SSL context', type=str)
define('smtp-ssl-keyfile', default=None, help='SMTP SSL key file', type=str)
define('smtp-ssl-certfile', default=None, help='SMTP SSL cert file', type=str)
define('smtp-ssl-context', default=None, help='SMTP SSL context', type=str)
define('smtp-username', default=None, help='SMTP username', type=str)
define('smtp-password', default=None, help='SMTP password', type=str)
define('debug', default=False, help='run in debug mode', type=bool)
define('config', help='read configuration file',
callback=lambda path: tornado.options.parse_config_file(path, final=False))