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

Add support for Reply-To. #58

Merged
merged 2 commits into from
Sep 21, 2017
Merged
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
30 changes: 25 additions & 5 deletions sgbackend/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,31 @@ def _build_sg_mail(self, email):
if hasattr(email, 'template_id'):
mail.set_template_id(email.template_id)
if hasattr(email, 'substitutions'):
for k, v in email.substitutions.items():
personalization.add_substitution(Substitution(k, v))

for k, v in email.extra_headers.items():
mail.add_header({k: v})
for key, value in email.substitutions.items():
personalization.add_substitution(Substitution(key, value))

# SendGrid does not support adding Reply-To as an extra
# header, so it needs to be manually removed if it exists.
reply_to_string = ""
for key, value in email.extra_headers.items():
if key.lower() == "reply-to":
reply_to_string = value
else:
mail.add_header({key: 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:
# SendGrid only supports setting Reply-To to a single address.
# See https://github.com/sendgrid/sendgrid-csharp/issues/339.
reply_to_string = email.reply_to[0]
# Determine whether reply_to contains a name and email address, or
# just an email address.
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))
elif reply_to_email:
mail.set_reply_to(Email(reply_to_email))

for attachment in email.attachments:
if isinstance(attachment, MIMEBase):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,45 @@ def test_build_w_bcc_sg_email(self):
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

def test_build_w_reply_to_sg_email(self):
# Test setting a Reply-To header.
msg = EmailMessage()
msg.extra_headers = {'Reply-To': 'andrii.soldatenko@test.com'}
with self.settings(SENDGRID_API_KEY='test_key'):
mail = SendGridBackend()._build_sg_mail(msg)
self.assertEqual(
mail,
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [{'subject': ''}],
'reply_to': {'email': 'andrii.soldatenko@test.com'},
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)
# Test using the reply_to attribute.
msg = EmailMessage(reply_to=('andrii.soldatenko@test.com',))
with self.settings(SENDGRID_API_KEY='test_key'):
mail = SendGridBackend()._build_sg_mail(msg)
self.assertEqual(
mail,
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [{'subject': ''}],
'reply_to': {'email': 'andrii.soldatenko@test.com'},
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)
# Test using "name <email>" format.
msg = EmailMessage(
reply_to=('Andrii Soldatenko <andrii.soldatenko@test.com>',))
with self.settings(SENDGRID_API_KEY='test_key'):
mail = SendGridBackend()._build_sg_mail(msg)
self.assertEqual(
mail,
{'content': [{'value': '', 'type': 'text/plain'}],
'personalizations': [{'subject': ''}],
'reply_to': {
'name': 'Andrii Soldatenko',
'email': 'andrii.soldatenko@test.com'},
'from': {'email': 'webmaster@localhost'}, 'subject': ''}
)

def test_build_empty_multi_alternatives_sg_email(self):
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives()
Expand Down