From 72d899460c1a48898175fc58cebe93659e3f5372 Mon Sep 17 00:00:00 2001 From: Lewis Taylor Date: Wed, 18 May 2016 19:01:54 +0100 Subject: [PATCH] SendGrid: Set to field when using merge_data (#14) * Set to field when using merge_data The `to` field is required even if providing recipient addresses in x-smtpapi. See https://sendgrid.com/docs/API_Reference/Web_API/mail.html#-send. * Check data['to'] contains expected emails * Add space for toname check * Make `to` expected data contain email only --- anymail/backends/sendgrid.py | 1 + tests/test_sendgrid_backend.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/anymail/backends/sendgrid.py b/anymail/backends/sendgrid.py index ee84b13..e3015d2 100644 --- a/anymail/backends/sendgrid.py +++ b/anymail/backends/sendgrid.py @@ -100,6 +100,7 @@ class SendGridPayload(RequestsPayload): self.set_recipients('to', self.to_list) else: # Merge-friendly smtpapi 'to' field + self.set_recipients('to', self.to_list) self.smtpapi['to'] = [email.address for email in self.to_list] self.all_recipients += self.to_list diff --git a/tests/test_sendgrid_backend.py b/tests/test_sendgrid_backend.py index 1267eda..3c2f905 100644 --- a/tests/test_sendgrid_backend.py +++ b/tests/test_sendgrid_backend.py @@ -431,8 +431,8 @@ class SendGridBackendAnymailFeatureTests(SendGridBackendMockAPITestCase): data = self.get_api_call_data() smtpapi = self.get_smtpapi() - self.assertNotIn('to', data) # recipients should be moved to smtpapi-to with merge_data - self.assertNotIn('toname', data) + self.assertEqual(data['toname'], [' ', 'Bob']) + self.assertEqual(data['to'], ['alice@example.com', 'bob@example.com']) self.assertEqual(smtpapi['to'], ['alice@example.com', 'Bob ']) self.assertEqual(smtpapi['sub'], { ':name': ["Alice", "Bob"],