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

From field in the mail headers can be different to the From in the envelope #38

Open
wants to merge 1 commit 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
5 changes: 3 additions & 2 deletions envelopes/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ def _connect(self, replace_current=False):
if self._login:
self._conn.login(self._login, self._password or '')

def send(self, envelope):
def send(self, envelope, return_path=None):
"""Sends an *envelope*."""
if not self.is_connected:
self._connect()

msg = envelope.to_mime_message()
smtp_from = return_path or msg["From"]
to_addrs = [envelope._addrs_to_header([addr]) for addr in envelope._to + envelope._cc + envelope._bcc]

return self._conn.sendmail(msg['From'], to_addrs, msg.as_string())
return self._conn.sendmail(smtp_from, to_addrs, msg.as_string())


class GMailSMTP(SMTP):
Expand Down
35 changes: 35 additions & 0 deletions tests/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,38 @@ def test_send(self):
assert call_args[0] == mime_msg['From']
assert call_args[1] == [envelope._addrs_to_header([addr]) for addr in envelope._to + envelope._cc + envelope._bcc]
assert call_args[2] != ''

def test_send(self):
conn = SMTP('localhost')

msg = self._dummy_message()
envelope = Envelope(**msg)
mime_msg = envelope.to_mime_message()

conn.send(envelope)
assert conn._conn is not None
assert len(conn._conn._call_stack.get('sendmail', [])) == 1

call_args = conn._conn._call_stack['sendmail'][0][0]
assert len(call_args) == 3
assert call_args[0] == mime_msg['From']
assert call_args[1] == [envelope._addrs_to_header([addr]) for addr in envelope._to + envelope._cc + envelope._bcc]
assert call_args[2] != ''

def test_send_with_return_path(self):
conn = SMTP('localhost')

msg = self._dummy_message()
envelope = Envelope(**msg)
mime_msg = envelope.to_mime_message()
FROM_USED_BY_SMTP = "[email protected]"

conn.send(envelope, FROM_USED_BY_SMTP)
assert conn._conn is not None
assert len(conn._conn._call_stack.get('sendmail', [])) == 1

call_args = conn._conn._call_stack['sendmail'][0][0]
assert len(call_args) == 3
assert call_args[0] == FROM_USED_BY_SMTP
assert call_args[1] == [envelope._addrs_to_header([addr]) for addr in envelope._to + envelope._cc + envelope._bcc]
assert call_args[2] != ''