diff options
Diffstat (limited to 'service/pixelated')
-rw-r--r-- | service/pixelated/adapter/services/mail_sender.py | 6 | ||||
-rw-r--r-- | service/pixelated/adapter/services/mail_service.py | 8 | ||||
-rw-r--r-- | service/pixelated/resources/mails_resource.py | 18 |
3 files changed, 23 insertions, 9 deletions
diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index d29b7d49..24ae839d 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -22,7 +22,7 @@ from twisted.internet import reactor from pixelated.support.functional import flatten -class MailSender(): +class MailSender(object): def __init__(self, account_email_address, smtp_client=None): self.account_email_address = account_email_address @@ -41,11 +41,11 @@ class MailSender(): def sendmail(self, mail): recipients = flatten([mail.to, mail.cc, mail.bcc]) - normalized_recepients = self.get_email_addresses(recipients) + normalized_recipients = self.get_email_addresses(recipients) resultDeferred = Deferred() senderFactory = SMTPSenderFactory( fromEmail=self.account_email_address, - toEmail=normalized_recepients, + toEmail=normalized_recipients, file=StringIO(mail.to_smtp_format()), deferred=resultDeferred) diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 6e309ee0..1e0a0414 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -55,10 +55,12 @@ class MailService: mail = InputMail.from_dict(content_dict) draft_id = content_dict.get('ident') - self.mail_sender.sendmail(mail) - sent_mail = self.move_to_sent(draft_id, mail) + def move_to_sent(_): + return self.move_to_sent(draft_id, mail) - return sent_mail + deferred = self.mail_sender.sendmail(mail) + deferred.addCallback(move_to_sent) + return deferred def move_to_sent(self, last_draft_ident, mail): if last_draft_ident: diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index 77a47cda..f387076b 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -1,7 +1,8 @@ import json from pixelated.adapter.model.mail import InputMail -from pixelated.resources import respond_json +from pixelated.resources import respond_json, respond_json_deferred from twisted.web.resource import Resource +from twisted.web import server from leap.common.events import ( register, events_pb2 as proto @@ -86,9 +87,20 @@ class MailsResource(Resource): def render_POST(self, request): content_dict = json.loads(request.content.read()) - sent_mail = self._mail_service.send_mail(content_dict) - return respond_json(sent_mail.as_dict(), request) + deferred = self._mail_service.send_mail(content_dict) + + def onSuccess(sent_mail): + data = sent_mail.as_dict() + respond_json_deferred(data, request) + + def onError(error): + respond_json_deferred({'message': str(error)}, request, status_code=422) + + deferred.addCallback(onSuccess) + deferred.addErrback(onError) + + return server.NOT_DONE_YET def render_PUT(self, request): content_dict = json.loads(request.content.read()) |