More specific error in SMTPBackend.send. Fixes #60

This commit is contained in:
Sergey Lavrinenko 2016-03-11 20:48:24 +03:00
parent b565bb42d2
commit 8c2ed66a96
3 changed files with 27 additions and 8 deletions

View File

@ -1,14 +1,15 @@
# encoding: utf-8
from __future__ import unicode_literals
__all__ = ['SMTPBackend', ]
import smtplib
import logging
from functools import wraps
from ..response import SMTPResponse
from .client import SMTPClientWithResponse, SMTPClientWithResponse_SSL
from ...utils import DNS_NAME
from .exceptions import SMTPConnectNetworkError
__all__ = ['SMTPBackend']
logger = logging.getLogger(__name__)
@ -85,16 +86,20 @@ class SMTPBackend:
def _send(self, **kwargs):
response = None
try:
client = self.get_client()
except (IOError, smtplib.SMTPException) as exc:
logger.exception("Error connecting smtp server")
except IOError as exc:
response = self.make_response(exception=SMTPConnectNetworkError.from_ioerror(exc))
except smtplib.SMTPException as exc:
response = self.make_response(exception=exc)
if response:
if not self.fail_silently:
response.raise_if_needed()
return response
return client.sendmail(**kwargs)
else:
return client.sendmail(**kwargs)
def sendmail(self, from_addr, to_addrs, msg, mail_options=None, rcpt_options=None):

View File

@ -0,0 +1,15 @@
# encoding: utf-8
from __future__ import unicode_literals
import socket
class SMTPConnectNetworkError(IOError):
"""Network error during connection establishment."""
@classmethod
def from_ioerror(cls, exc):
o = cls()
o.errno = exc.errno
o.filename = exc.filename
o.strerror = exc.strerror
return o

View File

@ -169,7 +169,6 @@ def test_external_urls():
success = 0
for url in [
'https://github.com/lavr/python-emails',
'http://yandex.com',
'http://www.smashingmagazine.com/'
]: