From a1b86ceeed727ac4aa923800866918c62cf97278 Mon Sep 17 00:00:00 2001 From: Brandon Dimcheff Date: Sun, 28 Feb 2016 12:48:01 -0500 Subject: [PATCH] send email from the alias instead of the main account The email provided in MAIL FROM is the main account's email address, even if you provide a specific "from" json key in the API call, while the FROM header is the email supplied via the api. This commit causes the MAIL FROM to match the supplied email and the FROM header. --- inbox/sendmail/smtp/postel.py | 18 +++++++++++------- tests/api/test_sending.py | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/inbox/sendmail/smtp/postel.py b/inbox/sendmail/smtp/postel.py index 9d5e62121..d98318d41 100644 --- a/inbox/sendmail/smtp/postel.py +++ b/inbox/sendmail/smtp/postel.py @@ -253,14 +253,17 @@ def smtp_password(self): self.log.info('SMTP Auth(Password) success') - def sendmail(self, recipients, msg): + def sendmail(self, recipients, msg, from_email=None): + if from_email is None: + from_email = self.email_address + try: return self.connection.sendmail( - self.email_address, recipients, msg) + from_email, recipients, msg) except UnicodeEncodeError: self.log.error('Unicode error when trying to decode email', logstash_tag='sendmail_encode_error', - email=self.email_address, recipients=recipients) + email=from_email, recipients=recipients) raise SendMailException( 'Invalid character in recipient address', 402) @@ -300,7 +303,7 @@ def __init__(self, account): # non-generic accounts have no smtp password self.auth_token = account.password - def _send(self, recipients, msg): + def _send(self, recipients, msg, from_email=None): """Send the email message. Retries up to SMTP_MAX_RETRIES times if the message couldn't be submitted to any recipient. @@ -319,7 +322,7 @@ def _send(self, recipients, msg): for _ in range(SMTP_MAX_RETRIES + 1): try: with self._get_connection() as smtpconn: - failures = smtpconn.sendmail(recipients, msg) + failures = smtpconn.sendmail(recipients, msg, from_email=from_email) if not failures: # Sending successful! return @@ -407,8 +410,9 @@ def send(self, draft): # from_addr is only ever a list with one element from_addr = draft.from_addr[0] + from_email = from_addr[1] msg = create_email(from_name=from_addr[0], - from_email=from_addr[1], + from_email=from_email, reply_to=draft.reply_to, inbox_uid=draft.inbox_uid, to_addr=draft.to_addr, @@ -422,7 +426,7 @@ def send(self, draft): recipient_emails = [email for name, email in itertools.chain( draft.to_addr, draft.cc_addr, draft.bcc_addr)] - self._send(recipient_emails, msg) + self._send(recipient_emails, msg, from_email=from_email) # Sent to all successfully self.log.info('Sending successful', sender=from_addr[1], diff --git a/tests/api/test_sending.py b/tests/api/test_sending.py index 21b9ca88b..580e981f2 100644 --- a/tests/api/test_sending.py +++ b/tests/api/test_sending.py @@ -67,7 +67,7 @@ def __enter__(self): def __exit__(self, exc_type, value, traceback): pass - def sendmail(self, recipients, msg): + def sendmail(self, recipients, msg, from_email=None): submitted_messages.append((recipients, msg)) monkeypatch.setattr('inbox.sendmail.smtp.postel.SMTPConnection', @@ -87,7 +87,7 @@ def __enter__(self): def __exit__(self, exc_type, value, traceback): pass - def sendmail(self, recipients, msg): + def sendmail(self, recipients, msg, from_email=None): raise exc_type(*args) return ErringSMTPConnection