Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Commit

Permalink
Add new MailSender based on OutgoingMail
Browse files Browse the repository at this point in the history
- Issue #499
- No longer needs local smtp port
  • Loading branch information
fbernitt committed Nov 3, 2015
1 parent 5c34683 commit af76313
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
26 changes: 25 additions & 1 deletion service/pixelated/adapter/services/mail_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from StringIO import StringIO
from email.utils import parseaddr
from leap.mail.outgoing.service import OutgoingMail

from twisted.internet.defer import Deferred, fail
from twisted.mail.smtp import SMTPSenderFactory
from twisted.internet import reactor
from twisted.internet import reactor, defer
from pixelated.support.functional import flatten


Expand All @@ -27,6 +28,29 @@ def __init__(self):
Exception.__init__(self, "Couldn't send mail now, try again later.")


class MailSender(object):

def __init__(self, account_email_address, keymanager, cert_path, remote_smtp_host, remote_smtp_port):
self._from = account_email_address
self._keymanager = keymanager
self._cert_path = cert_path
self._remote_smtp_host = remote_smtp_host
self._remote_smtp_port = remote_smtp_port

def sendmail(self, mail):
recipients = flatten([mail.to, mail.cc, mail.bcc])
outgoing_mail = self._create_outgoing_mail()
deferreds = []

for recipient in recipients:
deferreds.append(outgoing_mail.send_message(mail.to_smtp_format(), recipient))

return defer.gatherResults(deferreds)

def _create_outgoing_mail(self):
return OutgoingMail(self._from, self._keymanager, self._cert_path, self._cert_path, self._remote_smtp_host, self._remote_smtp_port)


class LocalSmtpMailSender(object):

def __init__(self, account_email_address, smtp):
Expand Down
2 changes: 2 additions & 0 deletions service/pixelated/bitmask_libraries/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def _create_new_session(self, username, password):

smtp = LeapSmtp(self._provider, auth, nicknym.keymanager)

# TODO: Create the new mail sender based on what we have in available LeapSmtp, e.g. the certs

return LeapSession(self._provider, auth, mail_store, soledad, nicknym, smtp)

def _lookup_session(self, key):
Expand Down
29 changes: 27 additions & 2 deletions service/test/unit/adapter/services/test_mail_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,41 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from leap.mail.outgoing.service import OutgoingMail
from twisted.trial import unittest

from mockito import mock, when, verify, any, unstub
from pixelated.adapter.services.mail_sender import LocalSmtpMailSender, SMTPDownException
from pixelated.adapter.services.mail_sender import LocalSmtpMailSender, SMTPDownException, MailSender
from pixelated.adapter.model.mail import InputMail
from pixelated.support.functional import flatten
from test.support.test_helper import mail_dict
from twisted.internet import reactor
from twisted.internet import reactor, defer
from twisted.internet.defer import Deferred


class MailSenderTest(unittest.TestCase):

def setUp(self):
self._cert_path = u'/some/cert/path'
self._keymanager_mock = mock()
self._remote_smtp_host = 'some.host.test'
self._remote_smtp_port = 1234

def tearDown(self):
unstub()

def test_iterates_over_recipients(self):
sender = MailSender('someone@somedomain.tld', self._keymanager_mock, self._cert_path, self._remote_smtp_host, self._remote_smtp_port)
input_mail = InputMail.from_dict(mail_dict())

when(OutgoingMail).send_message(any(), any()).thenAnswer(lambda: defer.succeed(None))

sender.sendmail(input_mail)

for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]):
verify(OutgoingMail).send_message(any(), recipient)


class LocalSmtpMailSenderTest(unittest.TestCase):
def setUp(self):
self.smtp = mock()
Expand Down

0 comments on commit af76313

Please sign in to comment.