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

Implement timeout and the timeout exception on STMP connection attempts #10

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 13 additions & 3 deletions envelopes/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,26 @@
"""

import smtplib
import socket

__all__ = ['SMTP', 'GMailSMTP', 'SendGridSMTP', 'MailcatcherSMTP']
TimeoutException = socket.timeout

__all__ = ['SMTP', 'GMailSMTP', 'SendGridSMTP', 'MailcatcherSMTP',
'TimeoutException']


class SMTP(object):
"""Wrapper around :py:class:`smtplib.SMTP` class."""

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

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

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

if self._tls:
self._conn.starttls()
Expand Down