Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support for smtplib.SMTP_SSL #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions envelopes/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ class SMTP(object):
"""Wrapper around :py:class:`smtplib.SMTP` class."""

def __init__(self, host=None, port=25, login=None, password=None,
tls=False, timeout=None):
tls=False, smtps=False, timeout=None):
self._conn = None
self._host = host
self._port = port
self._login = login
self._password = password
self._tls = tls
self._smtps = smtps
self._timeout = timeout

@property
Expand All @@ -67,11 +68,13 @@ def _connect(self, replace_current=False):
except (AttributeError, smtplib.SMTPServerDisconnected):
pass

protocol = smtplib.SMTP_SSL if self._smtps else smtplib.SMTP

if self._timeout:
self._conn = smtplib.SMTP(self._host, self._port,
timeout=self._timeout)
self._conn = protocol(self._host, self._port,
timeout=self._timeout)
else:
self._conn = smtplib.SMTP(self._host, self._port)
self._conn = protocol(self._host, self._port)

if self._tls:
self._conn.starttls()
Expand Down
12 changes: 12 additions & 0 deletions lib/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def quit(self):
self.__append_call('quit', [], dict())


class MockSMTPSSL(MockSMTP):
"""A class that mocks ``smtp.SMTP_SSL``."""

default_port = smtplib.SMTP_SSL_PORT


class BaseTestCase(object):
"""Base class for Envelopes test cases."""

Expand All @@ -163,10 +169,16 @@ def _patch_smtplib(self):
self._orig_smtp = smtplib.SMTP
smtplib.SMTP = MockSMTP

self._orig_smtp_ssl = smtplib.SMTP_SSL
smtplib.SMTP_SSL = MockSMTPSSL

def _unpatch_smtplib(self):
if hasattr(self, '_orig_smtp'):
smtplib.SMTP = self._orig_smtp

if hasattr(self, '_orig_smtp_ssl'):
smtplib.SMTP_SSL = self._orig_smtp_ssl

def _dummy_message(self):
return dict({
'to_addr': ('[email protected]', 'Example To'),
Expand Down
18 changes: 16 additions & 2 deletions tests/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from envelopes.conn import SMTP
from envelopes.envelope import Envelope
from lib.testing import BaseTestCase
from smtplib import SMTP_PORT, SMTP_SSL_PORT


class Test_SMTPConnection(BaseTestCase):
Expand All @@ -38,26 +39,28 @@ def setUp(self):

def test_constructor(self):
conn = SMTP('localhost', port=587, login='spam',
password='eggs', tls=True, timeout=10)
password='eggs', tls=True, smtps=True, timeout=10)

assert conn._conn is None
assert conn._host == 'localhost'
assert conn._port == 587
assert conn._login == 'spam'
assert conn._password == 'eggs'
assert conn._tls is True
assert conn._smtps is True
assert conn._timeout == 10

def test_constructor_all_kwargs(self):
conn = SMTP(host='localhost', port=587, login='spam',
password='eggs', tls=True)
password='eggs', tls=True, smtps=True)

assert conn._conn is None
assert conn._host == 'localhost'
assert conn._port == 587
assert conn._login == 'spam'
assert conn._password == 'eggs'
assert conn._tls is True
assert conn._smtps is True

def test_connect(self):
conn = SMTP('localhost')
Expand Down Expand Up @@ -89,6 +92,17 @@ def test_connect_starttls(self):
assert conn._conn is not None
assert len(conn._conn._call_stack.get('starttls', [])) == 1

def test_connect_smtps(self):
conn = SMTP('localhost', smtps=False)
conn._connect()
assert conn._conn is not None
assert conn._conn.default_port == SMTP_PORT

conn = SMTP('localhost', smtps=True)
conn._connect()
assert conn._conn is not None
assert conn._conn.default_port == SMTP_SSL_PORT

def test_connect_login(self):
conn = SMTP('localhost')
conn._connect()
Expand Down