Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Mail.add_header doesn't accept a dict #1054

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions sendgrid/helpers/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,13 @@ def add_header(self, header):
"""Add headers to the email globaly or to a specific Personalization

:param value: A Header object or a dict of header key/values
If dict has multiple keys, only first key/value item are applied
:type value: Header, dict
"""
if isinstance(header, dict):
(k, v) = list(header.items())[0]
self._headers = self._ensure_append(Header(k, v), self._headers)
return
if header.personalization is not None:
try:
personalization = \
Expand All @@ -471,23 +476,14 @@ def add_header(self, header):
except IndexError:
personalization = Personalization()
has_internal_personalization = False
if isinstance(header, dict):
(k, v) = list(header.items())[0]
personalization.add_header(Header(k, v))
else:
personalization.add_header(header)
personalization.add_header(header)

if not has_internal_personalization:
self.add_personalization(
personalization,
index=header.personalization)
else:
if isinstance(header, dict):
(k, v) = list(header.items())[0]
self._headers = self._ensure_append(
Header(k, v), self._headers)
else:
self._headers = self._ensure_append(header, self._headers)
self._headers = self._ensure_append(header, self._headers)

@property
def substitution(self):
Expand Down
138 changes: 138 additions & 0 deletions test/unit/test_mail_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import unittest

from sendgrid.helpers.mail.header import Header

try:
from email.message import EmailMessage
except ImportError:
Expand Down Expand Up @@ -338,6 +340,142 @@ def test_single_email_with_all_three_email_contents_to_single_recipient(self):
json.loads(response_content_with_all_three_mime_contents)
)

def test_single_email_with_custom_header_globaly(self):
from sendgrid.helpers.mail import (Mail, From, To, Subject,
Header, PlainTextContent)
self.maxDiff = None
message = Mail(
from_email=From('[email protected]', 'Example From Name'),
to_emails=To('[email protected]', 'Example To Name'),
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python')
)

message.header = Header('X-Test1', 'Test1')
response_content = json.dumps({
"content": [
{
"type": "text/plain",
"value": "and easy to do anywhere, even with Python"
}
],
"from": {
"email": "[email protected]",
"name": "Example From Name"
},
"personalizations": [
{
"to": [
{
"email": "[email protected]",
"name": "Example To Name"
}
]
}
],
"subject": "Sending with SendGrid is Fun",
"headers": {
"X-Test1": "Test1"
}
})
self.assertEqual(
message.get(),
json.loads(response_content)
)

def test_single_email_with_custom_header_globaly_single_key_value_dict(self):
from sendgrid.helpers.mail import (Mail, From, To, Subject,
Header, PlainTextContent)
self.maxDiff = None
message = Mail(
from_email=From('[email protected]', 'Example From Name'),
to_emails=To('[email protected]', 'Example To Name'),
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python')
)
message.add_header({'X-Test1': 'Test1'})

response_content = json.dumps({
"content": [
{
"type": "text/plain",
"value": "and easy to do anywhere, even with Python"
}
],
"from": {
"email": "[email protected]",
"name": "Example From Name"
},
"personalizations": [
{
"to": [
{
"email": "[email protected]",
"name": "Example To Name"
}
]
}
],
"subject": "Sending with SendGrid is Fun",
"headers": {
"X-Test1": "Test1"
}
})
self.assertEqual(
message.get(),
json.loads(response_content)
)

def test_single_email_with_custom_header_globaly_multi_key_values_dict(self):
from sendgrid.helpers.mail import (Mail, From, To, Subject,
Header, PlainTextContent)
self.maxDiff = None
message = Mail(
from_email=From('[email protected]', 'Example From Name'),
to_emails=To('[email protected]', 'Example To Name'),
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent(
'and easy to do anywhere, even with Python')
)
message.add_header({
'X-Test1': 'Test1',
'X-Test2': 'Test2'
})

response_content = json.dumps({
"content": [
{
"type": "text/plain",
"value": "and easy to do anywhere, even with Python"
}
],
"from": {
"email": "[email protected]",
"name": "Example From Name"
},
"personalizations": [
{
"to": [
{
"email": "[email protected]",
"name": "Example To Name"
}
]
}
],
"subject": "Sending with SendGrid is Fun",
"headers": {
"X-Test1": "Test1",
# "X-Test2": "Test2", # does NOT applied
}
})
self.assertEqual(
message.get(),
json.loads(response_content)
)

def test_single_email_with_amp_and_html_contents_to_single_recipient(self):
from sendgrid.helpers.mail import (Mail, From, To, Subject,
PlainTextContent, HtmlContent, AmpHtmlContent)
Expand Down