From 36461e57b95abf51f463dda53e2a1feba061b15f Mon Sep 17 00:00:00 2001 From: medmunds Date: Fri, 29 Apr 2016 15:05:58 -0700 Subject: [PATCH] Keep angle brackets on SendGrid smtp-id SendGrid seems to consistently use on Message-ID and smtp-id. --- anymail/backends/sendgrid.py | 5 +---- tests/test_sendgrid_backend.py | 10 +++++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/anymail/backends/sendgrid.py b/anymail/backends/sendgrid.py index 2920a90..56b45bb 100644 --- a/anymail/backends/sendgrid.py +++ b/anymail/backends/sendgrid.py @@ -1,5 +1,3 @@ -from email.utils import unquote - from django.core.mail import make_msgid from requests.structures import CaseInsensitiveDict @@ -115,8 +113,7 @@ class SendGridPayload(RequestsPayload): # Workaround for missing message ID (smtp-id) in SendGrid engagement events # (click and open tracking): because unique_args get merged into the raw event # record, we can supply the 'smtp-id' field for any events missing it. - # Must use the unquoted (no ) version to match other SendGrid APIs. - self.smtpapi.setdefault('unique_args', {})['smtp-id'] = unquote(self.message_id) + self.smtpapi.setdefault('unique_args', {})['smtp-id'] = self.message_id def make_message_id(self): """Returns a Message-ID that could be used for this payload diff --git a/tests/test_sendgrid_backend.py b/tests/test_sendgrid_backend.py index 955fe7d..01b9b9d 100644 --- a/tests/test_sendgrid_backend.py +++ b/tests/test_sendgrid_backend.py @@ -64,9 +64,9 @@ class SendGridBackendStandardEmailTests(SendGridBackendMockAPITestCase): # make sure backend assigned a Message-ID for event tracking email_headers = json.loads(data['headers']) self.assertRegex(email_headers['Message-ID'], r'\<.+@sender\.example\.com\>') # id uses from_email's domain - # make sure we added the unquoted Message-ID to unique_args for event notification + # make sure we added the Message-ID to unique_args for event notification smtpapi = self.get_smtpapi() - self.assertEqual(email_headers['Message-ID'], '<{}>'.format(smtpapi['unique_args']['smtp-id'])) + self.assertEqual(email_headers['Message-ID'], smtpapi['unique_args']['smtp-id']) @override_settings(ANYMAIL={'SENDGRID_USERNAME': 'sg_username', 'SENDGRID_PASSWORD': 'sg_password'}) def test_user_pass_auth(self): @@ -110,7 +110,7 @@ class SendGridBackendStandardEmailTests(SendGridBackendMockAPITestCase): cc=['cc1@example.com', 'Also CC '], headers={'Reply-To': 'another@example.com', 'X-MyHeader': 'my value', - 'Message-ID': 'mycustommsgid@sales.example.com'}) # should override backend msgid + 'Message-ID': ''}) # should override backend msgid email.send() data = self.get_api_call_data() self.assertEqual(data['subject'], "Subject") @@ -123,13 +123,13 @@ class SendGridBackendStandardEmailTests(SendGridBackendMockAPITestCase): self.assertEqual(data['cc'], ['cc1@example.com', 'cc2@example.com']) self.assertEqual(data['ccname'], [' ', 'Also CC']) self.assertJSONEqual(data['headers'], { - 'Message-ID': 'mycustommsgid@sales.example.com', + 'Message-ID': '', 'Reply-To': 'another@example.com', 'X-MyHeader': 'my value', }) # make sure custom Message-ID also added to unique_args self.assertJSONEqual(data['x-smtpapi'], { - 'unique_args': {'smtp-id': 'mycustommsgid@sales.example.com'} + 'unique_args': {'smtp-id': ''} }) def test_html_message(self):