diff --git a/anymail/backends/sendgrid.py b/anymail/backends/sendgrid.py index e3015d2..9bf2232 100644 --- a/anymail/backends/sendgrid.py +++ b/anymail/backends/sendgrid.py @@ -69,7 +69,7 @@ class SendGridPayload(RequestsPayload): 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 @@ -95,14 +95,10 @@ class SendGridPayload(RequestsPayload): 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: @@ -200,9 +196,8 @@ class SendGridPayload(RequestsPayload): 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"] diff --git a/tests/test_sendgrid_backend.py b/tests/test_sendgrid_backend.py index 3c2f905..23e1ef2 100644 --- a/tests/test_sendgrid_backend.py +++ b/tests/test_sendgrid_backend.py @@ -431,6 +431,7 @@ class SendGridBackendAnymailFeatureTests(SendGridBackendMockAPITestCase): 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 ']) diff --git a/tests/test_sendgrid_integration.py b/tests/test_sendgrid_integration.py index 3bc46a8..15f3bf9 100644 --- a/tests/test_sendgrid_integration.py +++ b/tests/test_sendgrid_integration.py @@ -89,6 +89,25 @@ class SendGridBackendIntegrationTests(SimpleTestCase, AnymailTestMixin): 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 ", + to=["to1@sink.sendgrid.net", "Recipient 2 "], + 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: