from __future__ import print_function, unicode_literals
+import socket
+import logging
import six
import smtplib
import sys
EMAIL_TEST_MBOX_INBOX = []
+class MailError(Exception):
+ """ General exception for mail errors """
+
+
+class NoSMTPServerError(MailError):
+ pass
+
+
class FakeMhost(object):
"""
Just a fake mail host so we can capture and test messages
else:
smtp_init = smtplib.SMTP
- mhost = smtp_init(
- mg_globals.app_config['email_smtp_host'],
- mg_globals.app_config['email_smtp_port'])
+ try:
+ mhost = smtp_init(
+ 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
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:
mhost.starttls()