7 Commits 678a8bccc5 ... 1ddfb76c85

Author SHA1 Message Date
  Davide Alberani 1ddfb76c85 improve SMTP SSL support 2 years ago
  Davide Alberani 89ed643f6b #16 fix login for SMTP 2 years ago
  Davide Alberani fbd2988936 #16 add Date header to email 2 years ago
  Davide Alberani 3563638dab #16 fix starttls 2 years ago
  Davide Alberani 06ac588f74 #16 avoid mixing parameters 2 years ago
  Davide Alberani 46de442890 #16: fix for None values 2 years ago
  Davide Alberani 2a88d5ab7a fixes #16: support SMTP authentication 2 years ago
2 changed files with 27 additions and 6 deletions
  1. 2 0
      conf/diffido.conf
  2. 25 6
      diffido.py

+ 2 - 0
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=''

+ 25 - 6
diffido.py

@@ -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))