Custom exception in mail.
This commit is contained in:
parent
9d37bcd06f
commit
d4eadc9461
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
from __future__ import print_function, unicode_literals
|
from __future__ import print_function, unicode_literals
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import logging
|
||||||
import six
|
import six
|
||||||
import smtplib
|
import smtplib
|
||||||
import sys
|
import sys
|
||||||
@ -54,6 +56,14 @@ EMAIL_TEST_INBOX = []
|
|||||||
EMAIL_TEST_MBOX_INBOX = []
|
EMAIL_TEST_MBOX_INBOX = []
|
||||||
|
|
||||||
|
|
||||||
|
class MailError(Exception):
|
||||||
|
""" General exception for mail errors """
|
||||||
|
|
||||||
|
|
||||||
|
class NoSMTPServerError(MailError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FakeMhost(object):
|
class FakeMhost(object):
|
||||||
"""
|
"""
|
||||||
Just a fake mail host so we can capture and test messages
|
Just a fake mail host so we can capture and test messages
|
||||||
@ -101,13 +111,27 @@ def send_email(from_addr, to_addrs, subject, message_body):
|
|||||||
else:
|
else:
|
||||||
smtp_init = smtplib.SMTP
|
smtp_init = smtplib.SMTP
|
||||||
|
|
||||||
mhost = smtp_init(
|
try:
|
||||||
mg_globals.app_config['email_smtp_host'],
|
mhost = smtp_init(
|
||||||
mg_globals.app_config['email_smtp_port'])
|
mg_globals.app_config['email_smtp_host'],
|
||||||
|
mg_globals.app_config['email_smtp_port'])
|
||||||
|
except socket.error:
|
||||||
|
error_message = "Couldn't contact mail server on <{}>:<{}>".format(
|
||||||
|
mg_globals.app_config['email_smtp_host'],
|
||||||
|
mg_globals.app_config['email_smtp_port'])
|
||||||
|
logging.debug(error_message)
|
||||||
|
raise NoSMTPServerError(error_message)
|
||||||
|
|
||||||
# SMTP.__init__ Issues SMTP.connect implicitly if host
|
# SMTP.__init__ Issues SMTP.connect implicitly if host
|
||||||
if not mg_globals.app_config['email_smtp_host']: # e.g. host = ''
|
if not mg_globals.app_config['email_smtp_host']: # e.g. host = ''
|
||||||
mhost.connect() # We SMTP.connect explicitly
|
try:
|
||||||
|
mhost.connect() # We SMTP.connect explicitly
|
||||||
|
except socket.error:
|
||||||
|
error_message = "Couldn't contact mail server on <{}>:<{}>".format(
|
||||||
|
mg_globals.app_config['email_smtp_host'],
|
||||||
|
mg_globals.app_config['email_smtp_port'])
|
||||||
|
logging.debug(error_message)
|
||||||
|
raise NoSMTPServerError(error_message)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mhost.starttls()
|
mhost.starttls()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user