Skip to content

Commit

Permalink
Postmark: handle Reply-To in EmailMessage headers
Browse files Browse the repository at this point in the history
Move 'Reply-To' header into dedicated Postmark API param

Fixes #39
  • Loading branch information
medmunds committed Nov 1, 2016
1 parent e78410e commit 4ca39a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 6 additions & 1 deletion anymail/backends/postmark.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re

from requests.structures import CaseInsensitiveDict

from ..exceptions import AnymailRequestsAPIError
from ..message import AnymailRecipientStatus
from ..utils import get_anymail_setting
Expand Down Expand Up @@ -140,9 +142,12 @@ def set_reply_to(self, emails):
self.data["ReplyTo"] = reply_to

def set_extra_headers(self, headers):
header_dict = CaseInsensitiveDict(headers)
if 'Reply-To' in header_dict:
self.data["ReplyTo"] = header_dict.pop('Reply-To')
self.data["Headers"] = [
{"Name": key, "Value": value}
for key, value in headers.items()
for key, value in header_dict.items()
]

def set_text_body(self, body):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_postmark_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def test_email_message(self):
self.assertEqual(data['To'], 'to1@example.com, Also To <to2@example.com>')
self.assertEqual(data['Bcc'], 'bcc1@example.com, Also BCC <bcc2@example.com>')
self.assertEqual(data['Cc'], 'cc1@example.com, Also CC <cc2@example.com>')
self.assertEqual(data['ReplyTo'], 'another@example.com')
self.assertCountEqual(data['Headers'], [
{'Name': 'Message-ID', 'Value': 'mycustommsgid@sales.example.com'},
{'Name': 'Reply-To', 'Value': 'another@example.com'},
{'Name': 'X-MyHeader', 'Value': 'my value'},
])

Expand Down Expand Up @@ -136,6 +136,16 @@ def test_reply_to(self):
self.assertEqual(data['ReplyTo'], 'reply@example.com, Other <reply2@example.com>')
self.assertEqual(data['Headers'], [{'Name': 'X-Other', 'Value': 'Keep'}]) # don't lose other headers

def test_reply_to_header(self):
# Reply-To needs to be moved out of headers, into dedicated param
email = mail.EmailMessage('Subject', 'Body goes here', 'from@example.com', ['to1@example.com'],
headers={'reply-to': 'reply@example.com, Other <reply2@example.com>',
'X-Other': 'Keep'})
email.send()
data = self.get_api_call_json()
self.assertEqual(data['ReplyTo'], 'reply@example.com, Other <reply2@example.com>')
self.assertEqual(data['Headers'], [{'Name': 'X-Other', 'Value': 'Keep'}]) # don't lose other headers

def test_attachments(self):
text_content = "* Item one\n* Item two\n* Item three"
self.message.attach(filename="test.txt", content=text_content, mimetype="text/plain")
Expand Down

0 comments on commit 4ca39a9

Please sign in to comment.