Compare commits
7 commits
678a8bccc5
...
1ddfb76c85
Author | SHA1 | Date | |
---|---|---|---|
1ddfb76c85 | |||
89ed643f6b | |||
fbd2988936 | |||
3563638dab | |||
06ac588f74 | |||
46de442890 | |||
2a88d5ab7a |
2 changed files with 27 additions and 6 deletions
|
@ -8,3 +8,5 @@ smtp_host='localhost'
|
||||||
#smtp_ssl_keyfile=None
|
#smtp_ssl_keyfile=None
|
||||||
#smtp_ssl_certfile=None
|
#smtp_ssl_certfile=None
|
||||||
#smtp_ssl_context=None
|
#smtp_ssl_context=None
|
||||||
|
#smtp_username=''
|
||||||
|
#smtp_password=''
|
||||||
|
|
31
diffido.py
31
diffido.py
|
@ -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,11 +294,14 @@ 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')
|
||||||
|
password = SMTP_SETTINGS.get('smtp-password')
|
||||||
args = {}
|
args = {}
|
||||||
for key, value in SMTP_SETTINGS.items():
|
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
|
continue
|
||||||
if key in ('smtp-port'):
|
if key in ('smtp-port'):
|
||||||
value = int(value)
|
value = int(value)
|
||||||
|
@ -305,18 +309,31 @@ def send_email(to, subject='diffido', body='', from_=None):
|
||||||
args[key] = value
|
args[key] = value
|
||||||
try:
|
try:
|
||||||
if use_ssl:
|
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:
|
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)
|
s.send_message(msg)
|
||||||
else:
|
else:
|
||||||
tls_args = {}
|
tls_args = {}
|
||||||
for key in ('ssl_keyfile', 'ssl_certfile', 'ssl_context'):
|
for key in ('ssl_keyfile', 'ssl_certfile', 'ssl_context'):
|
||||||
if key in args:
|
if key in args:
|
||||||
tls_args = 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 username:
|
||||||
|
logger.debug('STMP LOGIN for username %s and password of length %d' % (username, len(password)))
|
||||||
|
s.login(username, password)
|
||||||
s.send_message(msg)
|
s.send_message(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error('unable to send email to %s: %s' % (to, 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-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-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-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-keyfile', default=None, help='SMTP SSL key file', type=str)
|
||||||
define('smtp-ssl-certfile', default=None, help='SSL cert file', type=str)
|
define('smtp-ssl-certfile', default=None, help='SMTP SSL cert file', type=str)
|
||||||
define('smtp-ssl-context', default=None, help='SSL context', 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('debug', default=False, help='run in debug mode', type=bool)
|
||||||
define('config', help='read configuration file',
|
define('config', help='read configuration file',
|
||||||
callback=lambda path: tornado.options.parse_config_file(path, final=False))
|
callback=lambda path: tornado.options.parse_config_file(path, final=False))
|
||||||
|
|
Loading…
Reference in a new issue