changeset 259:d8b0a06cb763

Implement SMTPS mail delivery. For clarity the confibguration parameter MAIL_SMTP_USETLS has been renamed to MAIL_SMTP_STARTTLS and a new parameter MAIL_SMTP_SSL has been added to enable SMTPS.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 29 Sep 2014 15:08:23 +0200
parents 773d9567dcb2
children 0ac15efd8c17
files etc/ltpdarepo.ex src/ltpdarepo/mail.py src/ltpdarepo/tests/test_smtp.py
diffstat 3 files changed, 54 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/etc/ltpdarepo.ex	Mon Sep 29 13:52:20 2014 +0200
+++ b/etc/ltpdarepo.ex	Mon Sep 29 15:08:23 2014 +0200
@@ -10,7 +10,8 @@
 # mailserver
 MAIL_SMTP_SERVER = 'localhost'
 MAIL_SMTP_PORT = 25
-MAIL_SMTP_USETLS = False
+MAIL_SMTP_STARTTLS = False
+MAIL_SMTP_SSL = False
 MAIL_SMTP_USERNAME = ''
 MAIL_SMTP_PASSWORD = ''
 
--- a/src/ltpdarepo/mail.py	Mon Sep 29 13:52:20 2014 +0200
+++ b/src/ltpdarepo/mail.py	Mon Sep 29 15:08:23 2014 +0200
@@ -39,17 +39,21 @@
     def __init__(self, app):
         self.server   = app.config.get('MAIL_SMTP_SERVER', 'localhost')
         self.port     = app.config.get('MAIL_SMTP_PORT', 25)
-        self.usetls   = app.config.get('MAIL_SMTP_USETLS', False)
+        self.starttls = app.config.get('MAIL_SMTP_STARTTLS', False)
+        self.ssl      = app.config.get('MAIL_SMTP_SSL', False)
         self.username = app.config.get('MAIL_SMTP_USERNAME')
         self.password = app.config.get('MAIL_SMTP_PASSWORD')
         self.debug    = app.config.get('MAIL_SMTP_DEBUG')
 
     @contextmanager
     def connect(self):
-        smtp = smtplib.SMTP(self.server, self.port)
+        if self.ssl:
+            smtp = smtplib.SMTP_SSL(self.server, self.port)
+        else:
+            smtp = smtplib.SMTP(self.server, self.port)
         if self.debug:
             smtp.set_debuglevel(1)
-        if self.usetls:
+        if self.starttls:
             smtp.starttls()
         if self.username:
             smtp.login(self.username, self.password)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ltpdarepo/tests/test_smtp.py	Mon Sep 29 15:08:23 2014 +0200
@@ -0,0 +1,45 @@
+import unittest
+
+from ltpdarepo.mail import SMTPTransport
+
+# set to something sensible to run the tests in this file
+server = ''
+toaddr = ''
+user = ''
+passwd = ''
+
+
+# dummy app class to hold configuration
+class App(object):
+    def __init__(self, server, **kwargs):
+        self.config = {
+            'MAIL_SMTP_SERVER': server,
+            'MAIL_SMTP_PORT': 25,
+            'MAIL_SMTP_STARTTLS': False,
+            'MAIL_SMTP_SSL': False,
+            'MAIL_SMTP_USERNAME': '',
+            'MAIL_SMTP_PASSWORD': '',
+            'MAIL_SMTP_DEBUG': False, }
+        for k, v in kwargs.iteritems():
+            self.config['MAIL_SMTP_' + k.upper()] = v
+
+
+class TestCase(unittest.TestCase):
+
+    # @unittest.skipUnless(server, "requires smtp settings")
+    # def test_smtp(self):
+    #     app = App(server)
+    #     with SMTPTransport(app).connect() as smtp:
+    #         smtp.sendmail('Test <test@localhost>', toaddr, 'Test')
+
+    @unittest.skipUnless(server, "requires smtp settings")
+    def test_smtp_starttls(self):
+        app = App(server, port=587, starttls=True, username=user, password=passwd)
+        with SMTPTransport(app).connect() as smtp:
+            smtp.sendmail('Test <test@localhost>', toaddr, 'Test')
+        
+    @unittest.skipUnless(server, "requires smtp settings")
+    def test_smtp_ssl(self):
+        app = App(server, port=465, ssl=True, username=user, password=passwd)
+        with SMTPTransport(app).connect() as smtp:
+            smtp.sendmail('Test <test@localhost>', toaddr, 'Test')