Skip to content

Commit

Permalink
Sendinblue: fix "invalid headers" error
Browse files Browse the repository at this point in the history
Work around recent (unannounced) Sendinblue API change
that caused "Invalid headers" API error with non-string
custom header values, by converting basic numeric types
to strings. (Borrowed code from SendGrid backend.)

Fixes #288
  • Loading branch information
medmunds committed Nov 4, 2022
1 parent 448e8d9 commit 681a4af
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Fixes
* **Postmark:** Handle Postmark's SubscriptionChange events as Anymail
unsubscribe, subscribe, or bounce tracking events, rather than "unknown".
(Thanks to `@puru02`_ for the fix.)
* **Sendinblue:** Work around recent (unannounced) Sendinblue API change
that caused "Invalid headers" API error with non-string custom header
values. Anymail now converts int and float header values to strings.


Other
~~~~~
Expand Down
9 changes: 7 additions & 2 deletions anymail/backends/sendinblue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .base_requests import AnymailRequestsBackend, RequestsPayload
from ..exceptions import AnymailRequestsAPIError
from ..message import AnymailRecipientStatus
from ..utils import get_anymail_setting
from ..utils import get_anymail_setting, BASIC_NUMERIC_TYPES


class EmailBackend(AnymailRequestsBackend):
Expand Down Expand Up @@ -112,7 +112,12 @@ def set_reply_to(self, emails):
self.data['replyTo'] = self.email_object(emails[0])

def set_extra_headers(self, headers):
self.data['headers'].update(headers)
# SendinBlue requires header values to be strings -- not integers -- as of 11/2022.
# We'll stringify ints and floats; anything else is the caller's responsibility.
self.data["headers"].update({
k: str(v) if isinstance(v, BASIC_NUMERIC_TYPES) else v
for k, v in headers.items()
})

def set_tags(self, tags):
if len(tags) > 0:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_sendinblue_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def test_extra_headers(self):
self.message.send()
data = self.get_api_call_json()
self.assertEqual(data['headers']['X-Custom'], 'string')
self.assertEqual(data['headers']['X-Num'], 123)
# Header values must be strings (changed 11/2022)
self.assertEqual(data['headers']['X-Num'], "123")
# Reply-To must be moved to separate param
self.assertNotIn('Reply-To', data['headers'])
self.assertEqual(data['replyTo'], {'name': "Do Not Reply", 'email': "noreply@example.com"})
Expand Down

0 comments on commit 681a4af

Please sign in to comment.