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

Added in ability to add sections to a django email #63

Open
wants to merge 4 commits 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
37 changes: 37 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@ Example

mail.send()

Example of section usage
------------------------

.. code:: python


mail = EmailMultiAlternatives(
subject="A Subject",
body="This is a simple text email body.",
from_email="Some Person <[email protected]>",
to=["[email protected]"],
headers={"Reply-To": "[email protected]"}
)
# Add template
mail.template_id = 'YOUR TEMPLATE ID FROM SENDGRID ADMIN'

html_content = "<b>%subst-1%</b> and %subst-2%"
mail.attach_alternative(html_content, 'text/html', )

# Section substitutions you specify here will be used when the template is rendered
# See the 'section' docs @ sendgrid
mail.substitutions = {
'%subst-1%': 'Lets put %section-1%',
'%subst-2%': '%section-2%'
}
mail.sections = {
'%section-1%': 'some interesting content here',
'%section-2%': '(more interesting content, up to 10k bytes)',
}

mail.send()


Section usage result
----------
**Lets put some interesting content here**. Here is something else: (more interesting content, up to 10k bytes)


License
-------
Expand Down
11 changes: 8 additions & 3 deletions sgbackend/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

try:
from urllib.error import HTTPError # pragma: no cover
except ImportError: # pragma: no cover
except ImportError: # pragma: no cover
from urllib2 import HTTPError # pragma: no cover

try:
Expand All @@ -27,14 +27,15 @@
Email,
Mail,
Personalization,
Substitution
)
Substitution,
Section)


class SendGridBackend(BaseEmailBackend):
'''
SendGrid Web API Backend
'''

def __init__(self, fail_silently=False, **kwargs):
super(SendGridBackend, self).__init__(
fail_silently=fail_silently, **kwargs)
Expand Down Expand Up @@ -104,6 +105,10 @@ def _build_sg_mail(self, email):
for k, v in email.substitutions.items():
personalization.add_substitution(Substitution(k, v))

if hasattr(email, 'sections'):
for k, v in email.sections.items():
mail.add_section(Section(k, v))

for k, v in email.extra_headers.items():
mail.add_header({k: v})

Expand Down
14 changes: 14 additions & 0 deletions tests/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ def test_build_sg_email_w_substitutions(self):
'subject': ''}
)

def test_build_sg_email_w_sections(self):
msg = EmailMessage()
msg.sections = {'foo': 'bar'}
with self.settings(SENDGRID_API_KEY='test_key'):
mail = SendGridBackend()._build_sg_mail(msg)
self.assertEqual(
mail,
{'content': [{'type': 'text/plain', 'value': ''}],
'from': {'email': 'webmaster@localhost'},
'personalizations': [{'subject': ''}],
'sections': {'foo': 'bar'},
'subject': ''}
)

def test_build_sg_email_w_extra_headers(self):
msg = EmailMessage()
msg.extra_headers = {'EXTRA_HEADER': 'VALUE'}
Expand Down