From 2a88d5ab7a0ae9442345801ba81811e8dfa353ae Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Mon, 11 Oct 2021 20:32:56 +0200 Subject: [PATCH] fixes #16: support SMTP authentication --- conf/diffido.conf | 2 ++ diffido.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/conf/diffido.conf b/conf/diffido.conf index 7d5d23f..456a6b3 100644 --- a/conf/diffido.conf +++ b/conf/diffido.conf @@ -8,3 +8,5 @@ smtp_host='localhost' #smtp_ssl_keyfile=None #smtp_ssl_certfile=None #smtp_ssl_context=None +#smtp_username='' +#smtp_password='' diff --git a/diffido.py b/diffido.py index 614c31d..8793d00 100755 --- a/diffido.py +++ b/diffido.py @@ -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))