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

Updating function signatures and expected test outputs to match SG API v6 #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pytest>=3.6
pytest-cov
pytest-django
Django
sendgrid>=3.6.5,<4
sendgrid>=6.0.0,<7
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
description='SendGrid Backend for Django',
long_description=open('./README.rst').read(),
install_requires=[
"python_http_client >= 2.1.*, <2.3",
"sendgrid >= 3.5, <4",
"python_http_client >= 3, <4",
"sendgrid >= 6, <7",
],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
35 changes: 19 additions & 16 deletions sgbackend/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Mail,
Personalization,
Substitution,
Header,
)


Expand All @@ -48,7 +49,7 @@ def __init__(self, fail_silently=False, **kwargs):
raise ImproperlyConfigured('''
SENDGRID_API_KEY must be declared in settings.py''')

self.sg = sendgrid.SendGridAPIClient(apikey=self.api_key)
self.sg = sendgrid.SendGridAPIClient(api_key=self.api_key)
self.version = 'sendgrid/{0};django'.format(__version__)
self.sg.client.request_headers['User-agent'] = self.version

Expand All @@ -73,8 +74,8 @@ def send_messages(self, emails):
def _build_sg_mail(self, email):
mail = Mail()

mail.set_from(self._process_email_addr(email.from_email))
mail.set_subject(email.subject)
mail.from_email = self._process_email_addr(email.from_email)
mail.subject = email.subject

personalization = Personalization()
for e in email.to:
Expand All @@ -83,7 +84,7 @@ def _build_sg_mail(self, email):
personalization.add_cc(self._process_email_addr(e))
for e in email.bcc:
personalization.add_bcc(self._process_email_addr(e))
personalization.set_subject(email.subject)
personalization.subject = email.subject
mail.add_content(Content("text/plain", email.body))
if isinstance(email, EmailMultiAlternatives):
for alt in email.alternatives:
Expand All @@ -103,7 +104,7 @@ def _build_sg_mail(self, email):
mail.add_custom_arg(CustomArg(k, v))

if hasattr(email, 'template_id'):
mail.set_template_id(email.template_id)
mail.template_id = email.template_id
if hasattr(email, 'substitutions'):
for key, value in email.substitutions.items():
personalization.add_substitution(Substitution(key, value))
Expand All @@ -115,7 +116,7 @@ def _build_sg_mail(self, email):
if key.lower() == "reply-to":
reply_to_string = value
else:
mail.add_header({key: value})
mail.add_header(Header(key=key, value=value))
# Note that if you set a "Reply-To" header *and* the reply_to
# attribute, the header's value will be used.
if not mail.reply_to and hasattr(email, "reply_to") and email.reply_to:
Expand All @@ -127,25 +128,25 @@ def _build_sg_mail(self, email):
if reply_to_string:
reply_to_name, reply_to_email = rfc822.parseaddr(reply_to_string)
if reply_to_name and reply_to_email:
mail.set_reply_to(Email(reply_to_email, reply_to_name))
mail.reply_to = Email(reply_to_email, reply_to_name)
elif reply_to_email:
mail.set_reply_to(Email(reply_to_email))
mail.reply_to = Email(reply_to_email)

for attachment in email.attachments:
if isinstance(attachment, MIMEBase):
attach = Attachment()
attach.set_filename(attachment.get_filename())
attach.set_content(base64.b64encode(attachment.get_payload()))
attach.file_name = attachment.get_filename()
attach.file_content = base64.b64encode(attachment.get_payload())
mail.add_attachment(attach)
elif isinstance(attachment, tuple):
attach = Attachment()
attach.set_filename(attachment[0])
attach.file_name = attachment[0]
base64_attachment = base64.b64encode(attachment[1])
if sys.version_info >= (3,):
attach.set_content(str(base64_attachment, 'utf-8'))
attach.file_content = str(base64_attachment, 'utf-8')
else:
attach.set_content(base64_attachment)
attach.set_type(attachment[2])
attach.file_content = base64_attachment
attach.file_type = attachment[2]
mail.add_attachment(attach)

mail.add_personalization(personalization)
Expand All @@ -158,5 +159,7 @@ def _process_email_addr(self, email_addr):
# sendgrid/helpers/mail/mail.py:164
if not from_name:
from_name = None

return Email(from_email, from_name)
email = Email()
email.email = from_email
email.name = from_name
return email
34 changes: 16 additions & 18 deletions tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_build_empty_sg_mail(self):
{'from': {'email': 'webmaster@localhost'},
'subject': '',
'content': [{'type': 'text/plain', 'value': ''}],
'personalizations': [{'subject': ''}]}
'personalizations': [{}]}
)

def test_build_w_to_sg_email(self):
Expand All @@ -34,8 +34,7 @@ def test_build_w_to_sg_email(self):
mail,
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [
{'to': [{'email': '[email protected]'}],
'subject': ''}],
{'to': [{'email': '[email protected]'}],}],
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

Expand All @@ -49,8 +48,7 @@ def test_build_w_to_sg_email(self):
'personalizations': [
{'to': [
{'name': 'Andrii Soldatenko',
'email': '[email protected]'}],
'subject': ''}],
'email': '[email protected]'}],}],
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

Expand All @@ -63,7 +61,7 @@ def test_build_w_cc_sg_email(self):
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [
{'cc': [{'email': '[email protected]'}],
'subject': ''}],
}],
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

Expand All @@ -78,7 +76,7 @@ def test_build_w_cc_sg_email(self):
{'cc': [
{'name': 'Andrii Soldatenko',
'email': '[email protected]'}],
'subject': ''}],
}],
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

Expand All @@ -91,7 +89,7 @@ def test_build_w_bcc_sg_email(self):
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [
{'bcc': [{'email': '[email protected]'}],
'subject': ''}],
}],
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

Expand All @@ -106,7 +104,7 @@ def test_build_w_bcc_sg_email(self):
{'bcc': [
{'name': 'Andrii Soldatenko',
'email': '[email protected]'}],
'subject': ''}],
}],
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

Expand All @@ -119,7 +117,7 @@ def test_build_w_reply_to_sg_email(self):
self.assertEqual(
mail,
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'reply_to': {'email': '[email protected]'},
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)
Expand All @@ -130,7 +128,7 @@ def test_build_w_reply_to_sg_email(self):
self.assertEqual(
mail,
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'reply_to': {'email': '[email protected]'},
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)
Expand All @@ -142,7 +140,7 @@ def test_build_w_reply_to_sg_email(self):
self.assertEqual(
mail,
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'reply_to': {
'name': 'Andrii Soldatenko',
'email': '[email protected]'},
Expand All @@ -163,7 +161,7 @@ def test_build_empty_multi_alternatives_sg_email(self):
'<strong>important</strong> '
'message.</p>'}],
'from': {'email': 'webmaster@localhost'},
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'subject': ''}
)

Expand All @@ -177,7 +175,7 @@ def test_build_sg_email_w_categories(self):
{'categories': ['name'],
'content': [{'type': 'text/plain', 'value': ''}],
'from': {'email': 'webmaster@localhost'},
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'subject': ''
}
)
Expand All @@ -192,7 +190,7 @@ def test_build_sg_email_w_template_id(self):
{'template_id': 'template_id_123456',
'content': [{'type': 'text/plain', 'value': ''}],
'from': {'email': 'webmaster@localhost'},
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'subject': ''
}
)
Expand All @@ -206,7 +204,7 @@ def test_build_sg_email_w_substitutions(self):
mail,
{'content': [{'type': 'text/plain', 'value': ''}],
'from': {'email': 'webmaster@localhost'},
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'subject': ''}
)

Expand All @@ -220,7 +218,7 @@ def test_build_sg_email_w_extra_headers(self):
{'content': [{'type': 'text/plain', 'value': ''}],
'from': {'email': 'webmaster@localhost'},
'headers': {'EXTRA_HEADER': 'VALUE'},
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'subject': ''}
)

Expand All @@ -236,6 +234,6 @@ def test_build_sg_email_w_custom_args(self):
{'content': [{'type': 'text/plain', 'value': ''}],
'custom_args': {'custom_arg1': '12345-abcdef'},
'from': {'email': 'webmaster@localhost'},
'personalizations': [{'subject': ''}],
'personalizations': [{}],
'subject': ''}
)