aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tools/mail.py
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/tools/mail.py')
-rw-r--r--mediagoblin/tools/mail.py67
1 files changed, 53 insertions, 14 deletions
diff --git a/mediagoblin/tools/mail.py b/mediagoblin/tools/mail.py
index 0fabc5a9..3dc180d8 100644
--- a/mediagoblin/tools/mail.py
+++ b/mediagoblin/tools/mail.py
@@ -14,9 +14,15 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from __future__ import print_function, unicode_literals
+
+import socket
+import logging
+import six
import smtplib
-from email.MIMEText import MIMEText
+import sys
from mediagoblin import mg_globals, messages
+from mediagoblin._compat import MIMEText
from mediagoblin.tools import common
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -50,6 +56,14 @@ EMAIL_TEST_INBOX = []
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
@@ -64,6 +78,8 @@ class FakeMhost(object):
'to': to_addrs,
'message': message})
+ def starttls(self):
+ raise smtplib.SMTPException("No STARTTLS here")
def _clear_test_inboxes():
global EMAIL_TEST_INBOX
@@ -95,13 +111,34 @@ def send_email(from_addr, to_addrs, subject, message_body):
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 as original_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(original_error)
+ 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 as original_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(original_error)
+ raise NoSMTPServerError(error_message)
+
+ try:
+ mhost.starttls()
+ except smtplib.SMTPException:
+ # Only raise an exception if we're forced to
+ if mg_globals.app_config['email_smtp_force_starttls']:
+ six.reraise(*sys.exc_info())
if ((not common.TESTS_ENABLED)
and (mg_globals.app_config['email_smtp_user']
@@ -119,12 +156,12 @@ def send_email(from_addr, to_addrs, subject, message_body):
EMAIL_TEST_INBOX.append(message)
elif mg_globals.app_config['email_debug_mode']:
- print u"===== Email ====="
- print u"From address: %s" % message['From']
- print u"To addresses: %s" % message['To']
- print u"Subject: %s" % message['Subject']
- print u"-- Body: --"
- print message.get_payload(decode=True)
+ print("===== Email =====")
+ print("From address: %s" % message['From'])
+ print("To addresses: %s" % message['To'])
+ print("Subject: %s" % message['Subject'])
+ print("-- Body: --")
+ print(message_body)
return mhost.sendmail(from_addr, to_addrs, message.as_string())
@@ -150,6 +187,8 @@ def email_debug_message(request):
"""
if mg_globals.app_config['email_debug_mode']:
# DEBUG message, no need to translate
- messages.add_message(request, messages.DEBUG,
- u"This instance is running in email debug mode. "
- u"The email will be on the console of the server process.")
+ messages.add_message(
+ request,
+ messages.DEBUG,
+ "This instance is running in email debug mode. "
+ "The email will be on the console of the server process.")