# HG changeset patch # User Daniele Nicolodi # Date 1411996103 -7200 # Node ID d8b0a06cb7630fea8e1dee13dd08339cb9ec5a14 # Parent 773d9567dcb2b7d22163089aea04a3010675b344 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. diff -r 773d9567dcb2 -r d8b0a06cb763 etc/ltpdarepo.ex --- 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 = '' diff -r 773d9567dcb2 -r d8b0a06cb763 src/ltpdarepo/mail.py --- 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) diff -r 773d9567dcb2 -r d8b0a06cb763 src/ltpdarepo/tests/test_smtp.py --- /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 ', 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 ', 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 ', toaddr, 'Test')