Compare commits

...

7 commits

2 changed files with 27 additions and 6 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

@ -23,6 +23,7 @@ import shutil
import urllib
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
import logging
import datetime
import requests
@ -293,11 +294,14 @@ def send_email(to, subject='diffido', body='', from_=None):
msg['Subject'] = subject
msg['From'] = from_ or EMAIL_FROM
msg['To'] = to
msg["Date"] = formatdate(localtime=True)
starttls = SMTP_SETTINGS.get('smtp-starttls')
use_ssl = SMTP_SETTINGS.get('smtp-use-ssl')
username = SMTP_SETTINGS.get('smtp-username')
password = SMTP_SETTINGS.get('smtp-password')
args = {}
for key, value in SMTP_SETTINGS.items():
if key in ('smtp-starttls', 'smtp-use-ssl'):
if key in ('smtp-starttls', 'smtp-use-ssl', 'smtp-username', 'smtp-password'):
continue
if key in ('smtp-port'):
value = int(value)
@ -305,18 +309,31 @@ def send_email(to, subject='diffido', body='', from_=None):
args[key] = value
try:
if use_ssl:
for key in ('ssl_keyfile', 'ssl_certfile', 'ssl_context'):
if key in args:
args[key.replace('ssl_', '')] = args[key]
del args[key]
logger.debug('STMP SSL connection with args: %s' % repr(args))
with smtplib.SMTP_SSL(**args) as s:
if username:
logger.debug('STMP LOGIN for username %s and password of length %d' % (username, len(password)))
s.login(username, 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.replace('ssl_', '')] = args[key]
del args[key]
logger.debug('STMP connection with args: %s' % repr(args))
with smtplib.SMTP(**args) as s:
if starttls:
s.starttls(**tls_args)
logger.debug('STMP STARTTLS connection with args: %s' % repr(tls_args))
s.ehlo_or_helo_if_needed()
s.starttls(**tls_args)
if username:
logger.debug('STMP LOGIN for username %s and password of length %d' % (username, len(password)))
s.login(username, password)
s.send_message(msg)
except Exception as e:
logger.error('unable to send email to %s: %s' % (to, e))
@ -713,9 +730,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='', help='SMTP username', type=str)
define('smtp-password', default='', 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))