Skip to content

Commit

Permalink
SendGrid: improve to handling with merge_data
Browse files Browse the repository at this point in the history
* Add merge_data integration test
* Simplify backend handling for this case
  (also fixes duplicated to's in all_recipients
  from previous fix)
  • Loading branch information
medmunds committed May 18, 2016
1 parent 72d8994 commit 995617a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
17 changes: 6 additions & 11 deletions anymail/backends/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, message, defaults, backend, *args, **kwargs):
self.generate_message_id = backend.generate_message_id
self.message_id = None # Message-ID -- assigned in serialize_data unless provided in headers
self.smtpapi = {} # SendGrid x-smtpapi field
self.to_list = [] # late-bound 'to' field
self.to_list = [] # needed for build_merge_data
self.merge_field_format = backend.merge_field_format
self.merge_data = None # late-bound per-recipient data
self.merge_global_data = None
Expand All @@ -95,14 +95,10 @@ def serialize_data(self):
self.ensure_message_id()

self.build_merge_data()
if self.merge_data is None:
# Standard 'to' and 'toname' headers
self.set_recipients('to', self.to_list)
else:
# Merge-friendly smtpapi 'to' field
self.set_recipients('to', self.to_list)
if self.merge_data is not None:
# Must *also* set smtpapi 'to' field so SG does batch send
# (else all recipients would see each other's emails)
self.smtpapi['to'] = [email.address for email in self.to_list]
self.all_recipients += self.to_list

# Serialize x-smtpapi to json:
if len(self.smtpapi) > 0:
Expand Down Expand Up @@ -200,9 +196,8 @@ def set_from_email(self, email):
self.data["fromname"] = email.name

def set_to(self, emails):
# late-bind in self.serialize_data, because whether it goes in smtpapi
# depends on whether there is merge_data
self.to_list = emails
self.to_list = emails # track for later use by build_merge_data
self.set_recipients('to', emails)

def set_recipients(self, recipient_type, emails):
assert recipient_type in ["to", "cc", "bcc"]
Expand Down
1 change: 1 addition & 0 deletions tests/test_sendgrid_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ def test_merge_data(self):

data = self.get_api_call_data()
smtpapi = self.get_smtpapi()
# For batch send, must set both to+toname *and* smtpapi['to']:
self.assertEqual(data['toname'], [' ', 'Bob'])
self.assertEqual(data['to'], ['alice@example.com', 'bob@example.com'])
self.assertEqual(smtpapi['to'], ['alice@example.com', 'Bob <bob@example.com>'])
Expand Down
19 changes: 19 additions & 0 deletions tests/test_sendgrid_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ def test_all_options(self):
message.send()
self.assertEqual(message.anymail_status.status, {'queued'}) # SendGrid always queues

def test_merge_data(self):
message = AnymailMessage(
subject="Anymail merge_data test: %value%",
body="This body includes merge data: %value%",
from_email="Test From <from@example.com>",
to=["to1@sink.sendgrid.net", "Recipient 2 <to2@sink.sendgrid.net>"],
merge_data={
'to1@sink.sendgrid.net': {'value': 'one'},
'to2@sink.sendgrid.net': {'value': 'two'},
},
esp_extra={
'merge_field_format': '%{}%',
},
)
message.send()
recipient_status = message.anymail_status.recipients
self.assertEqual(recipient_status['to1@sink.sendgrid.net'].status, 'queued')
self.assertEqual(recipient_status['to2@sink.sendgrid.net'].status, 'queued')

@override_settings(ANYMAIL_SENDGRID_API_KEY="Hey, that's not an API key!")
def test_invalid_api_key(self):
with self.assertRaises(AnymailAPIError) as cm:
Expand Down

0 comments on commit 995617a

Please sign in to comment.