mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
SendGrid: set to to from in batch send.
Set ignored (but required-valid) `to` field to the from_email for batch send with merge_data. See https://github.com/anymail/django-anymail/pull/14#issuecomment-220231250
This commit is contained in:
@@ -96,9 +96,14 @@ class SendGridPayload(RequestsPayload):
|
|||||||
|
|
||||||
self.build_merge_data()
|
self.build_merge_data()
|
||||||
if self.merge_data is not None:
|
if self.merge_data is not None:
|
||||||
# Must *also* set smtpapi 'to' field so SG does batch send
|
# Move the 'to' recipients to smtpapi, so SG does batch send
|
||||||
# (else all recipients would see each other's emails)
|
# (else all recipients would see each other's emails).
|
||||||
|
# Regular 'to' must still be a valid email (even though "ignored")...
|
||||||
|
# we use the from_email as recommended by SG support
|
||||||
|
# (See https://github.com/anymail/django-anymail/pull/14#issuecomment-220231250)
|
||||||
self.smtpapi['to'] = [email.address for email in self.to_list]
|
self.smtpapi['to'] = [email.address for email in self.to_list]
|
||||||
|
self.data['to'] = [self.data['from']]
|
||||||
|
self.data['toname'] = [self.data.get('fromname', " ")]
|
||||||
|
|
||||||
# Serialize x-smtpapi to json:
|
# Serialize x-smtpapi to json:
|
||||||
if len(self.smtpapi) > 0:
|
if len(self.smtpapi) > 0:
|
||||||
|
|||||||
@@ -413,6 +413,7 @@ class SendGridBackendAnymailFeatureTests(SendGridBackendMockAPITestCase):
|
|||||||
})
|
})
|
||||||
|
|
||||||
def test_merge_data(self):
|
def test_merge_data(self):
|
||||||
|
self.message.from_email = 'from@example.com'
|
||||||
self.message.to = ['alice@example.com', 'Bob <bob@example.com>']
|
self.message.to = ['alice@example.com', 'Bob <bob@example.com>']
|
||||||
# SendGrid template_id is not required to use merge.
|
# SendGrid template_id is not required to use merge.
|
||||||
# You can just supply template content as the message (e.g.):
|
# You can just supply template content as the message (e.g.):
|
||||||
@@ -431,10 +432,12 @@ 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']:
|
# For batch send, smtpapi['to'] gets real recipient list;
|
||||||
self.assertEqual(data['toname'], [' ', 'Bob'])
|
# normal 'to' is not used (but must be valid, so we substitute the from_email):
|
||||||
self.assertEqual(data['to'], ['alice@example.com', 'bob@example.com'])
|
self.assertEqual(data['to'], ['from@example.com'])
|
||||||
|
self.assertEqual(data['toname'], [' ']) # empty string if no name in from_email
|
||||||
self.assertEqual(smtpapi['to'], ['alice@example.com', 'Bob <bob@example.com>'])
|
self.assertEqual(smtpapi['to'], ['alice@example.com', 'Bob <bob@example.com>'])
|
||||||
|
# smtpapi['sub'] values should be in to-list order:
|
||||||
self.assertEqual(smtpapi['sub'], {
|
self.assertEqual(smtpapi['sub'], {
|
||||||
':name': ["Alice", "Bob"],
|
':name': ["Alice", "Bob"],
|
||||||
':group': ["Developers", ":group"], # missing value gets replaced with var name...
|
':group': ["Developers", ":group"], # missing value gets replaced with var name...
|
||||||
|
|||||||
Reference in New Issue
Block a user