summaryrefslogtreecommitdiff
path: root/service/pixelated/adapter/services
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2015-02-18 13:40:41 -0200
committerDuda Dornelles <ddornell@thoughtworks.com>2015-02-18 13:40:49 -0200
commitdd63db87fdbb9667c1027edd700b4047b6983d1f (patch)
tree85c7a2875f535412760dc7d87c7550777f265814 /service/pixelated/adapter/services
parent4a8217744e1d1ca60d158c3497a09b92f857a7fd (diff)
#294 - not failing service startup if smtp fails to start
Diffstat (limited to 'service/pixelated/adapter/services')
-rw-r--r--service/pixelated/adapter/services/mail_sender.py36
1 files changed, 22 insertions, 14 deletions
diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py
index 24ae839d..9f42fbbc 100644
--- a/service/pixelated/adapter/services/mail_sender.py
+++ b/service/pixelated/adapter/services/mail_sender.py
@@ -16,14 +16,20 @@
from StringIO import StringIO
import re
-from twisted.internet.defer import Deferred
+from twisted.internet.defer import Deferred, fail
from twisted.mail.smtp import SMTPSenderFactory
from twisted.internet import reactor
from pixelated.support.functional import flatten
+class SMTPDownException(Exception):
+ def __init__(self):
+ Exception.__init__(self, "Couldn't send mail now, try again later.")
+
+
class MailSender(object):
- def __init__(self, account_email_address, smtp_client=None):
+ def __init__(self, account_email_address, ensure_smtp_is_running_cb):
+ self.ensure_smtp_is_running_cb = ensure_smtp_is_running_cb
self.account_email_address = account_email_address
def recepients_normalizer(self, mail_list):
@@ -40,15 +46,17 @@ class MailSender(object):
return self.recepients_normalizer(clean_mail_list)
def sendmail(self, mail):
- recipients = flatten([mail.to, mail.cc, mail.bcc])
- normalized_recipients = self.get_email_addresses(recipients)
- resultDeferred = Deferred()
- senderFactory = SMTPSenderFactory(
- fromEmail=self.account_email_address,
- toEmail=normalized_recipients,
- file=StringIO(mail.to_smtp_format()),
- deferred=resultDeferred)
-
- reactor.connectTCP('localhost', 4650, senderFactory)
-
- return resultDeferred
+ if self.ensure_smtp_is_running_cb():
+ recipients = flatten([mail.to, mail.cc, mail.bcc])
+ normalized_recipients = self.get_email_addresses(recipients)
+ resultDeferred = Deferred()
+ senderFactory = SMTPSenderFactory(
+ fromEmail=self.account_email_address,
+ toEmail=normalized_recipients,
+ file=StringIO(mail.to_smtp_format()),
+ deferred=resultDeferred)
+
+ reactor.connectTCP('localhost', 4650, senderFactory)
+
+ return resultDeferred
+ return fail(SMTPDownException())