Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send email from the alias instead of the main account #277

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions inbox/sendmail/smtp/postel.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,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)

Expand Down Expand Up @@ -314,7 +317,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.

Expand All @@ -333,7 +336,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
Expand Down Expand Up @@ -460,8 +463,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,
Expand All @@ -475,7 +479,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],
Expand Down
4 changes: 2 additions & 2 deletions tests/api/test_sending.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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',
Expand All @@ -88,7 +88,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
Expand Down