mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
SendGrid: improve to handling with merge_data
* Add merge_data integration test * Simplify backend handling for this case (also fixes duplicated to's in all_recipients from previous fix)
This commit is contained in:
@@ -69,7 +69,7 @@ class SendGridPayload(RequestsPayload):
|
|||||||
self.generate_message_id = backend.generate_message_id
|
self.generate_message_id = backend.generate_message_id
|
||||||
self.message_id = None # Message-ID -- assigned in serialize_data unless provided in headers
|
self.message_id = None # Message-ID -- assigned in serialize_data unless provided in headers
|
||||||
self.smtpapi = {} # SendGrid x-smtpapi field
|
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_field_format = backend.merge_field_format
|
||||||
self.merge_data = None # late-bound per-recipient data
|
self.merge_data = None # late-bound per-recipient data
|
||||||
self.merge_global_data = None
|
self.merge_global_data = None
|
||||||
@@ -95,14 +95,10 @@ class SendGridPayload(RequestsPayload):
|
|||||||
self.ensure_message_id()
|
self.ensure_message_id()
|
||||||
|
|
||||||
self.build_merge_data()
|
self.build_merge_data()
|
||||||
if self.merge_data is None:
|
if self.merge_data is not None:
|
||||||
# Standard 'to' and 'toname' headers
|
# Must *also* set smtpapi 'to' field so SG does batch send
|
||||||
self.set_recipients('to', self.to_list)
|
# (else all recipients would see each other's emails)
|
||||||
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.smtpapi['to'] = [email.address for email in self.to_list]
|
||||||
self.all_recipients += self.to_list
|
|
||||||
|
|
||||||
# Serialize x-smtpapi to json:
|
# Serialize x-smtpapi to json:
|
||||||
if len(self.smtpapi) > 0:
|
if len(self.smtpapi) > 0:
|
||||||
@@ -200,9 +196,8 @@ class SendGridPayload(RequestsPayload):
|
|||||||
self.data["fromname"] = email.name
|
self.data["fromname"] = email.name
|
||||||
|
|
||||||
def set_to(self, emails):
|
def set_to(self, emails):
|
||||||
# late-bind in self.serialize_data, because whether it goes in smtpapi
|
self.to_list = emails # track for later use by build_merge_data
|
||||||
# depends on whether there is merge_data
|
self.set_recipients('to', emails)
|
||||||
self.to_list = emails
|
|
||||||
|
|
||||||
def set_recipients(self, recipient_type, emails):
|
def set_recipients(self, recipient_type, emails):
|
||||||
assert recipient_type in ["to", "cc", "bcc"]
|
assert recipient_type in ["to", "cc", "bcc"]
|
||||||
|
|||||||
@@ -431,6 +431,7 @@ class SendGridBackendAnymailFeatureTests(SendGridBackendMockAPITestCase):
|
|||||||
|
|
||||||
data = self.get_api_call_data()
|
data = self.get_api_call_data()
|
||||||
smtpapi = self.get_smtpapi()
|
smtpapi = self.get_smtpapi()
|
||||||
|
# For batch send, must set both to+toname *and* smtpapi['to']:
|
||||||
self.assertEqual(data['toname'], [' ', 'Bob'])
|
self.assertEqual(data['toname'], [' ', 'Bob'])
|
||||||
self.assertEqual(data['to'], ['alice@example.com', 'bob@example.com'])
|
self.assertEqual(data['to'], ['alice@example.com', 'bob@example.com'])
|
||||||
self.assertEqual(smtpapi['to'], ['alice@example.com', 'Bob <bob@example.com>'])
|
self.assertEqual(smtpapi['to'], ['alice@example.com', 'Bob <bob@example.com>'])
|
||||||
|
|||||||
@@ -89,6 +89,25 @@ class SendGridBackendIntegrationTests(SimpleTestCase, AnymailTestMixin):
|
|||||||
message.send()
|
message.send()
|
||||||
self.assertEqual(message.anymail_status.status, {'queued'}) # SendGrid always queues
|
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!")
|
@override_settings(ANYMAIL_SENDGRID_API_KEY="Hey, that's not an API key!")
|
||||||
def test_invalid_api_key(self):
|
def test_invalid_api_key(self):
|
||||||
with self.assertRaises(AnymailAPIError) as cm:
|
with self.assertRaises(AnymailAPIError) as cm:
|
||||||
|
|||||||
Reference in New Issue
Block a user