An implementation of Django's EmailBackend compatible with sendgrid-python v5+
Project description
django-sendgrid-v5
This package implements an email backend for Django that relies on sendgrid's REST API for message delivery.
It is under active development, and pull requests are more than welcome!
To use the backend, simply install the package (using pip), set the EMAIL_BACKEND
setting in Django, and add a SENDGRID_API_KEY
key (set to the appropriate value) to your Django settings.
How to Install
pip install django-sendgrid-v5
- In your project's settings.py script:
- Set
EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
- Set the SENDGRID_API_KEY in settings.py to your api key that was provided to you by sendgrid.
SENDGRID_API_KEY = os.environ["SENDGRID_API_KEY"]
- Set
Other settings
- To toggle sandbox mode (when django is running in DEBUG mode), set
SENDGRID_SANDBOX_MODE_IN_DEBUG = True/False
.- To err on the side of caution, this defaults to True, so emails sent in DEBUG mode will not be delivered, unless this setting is explicitly set to False.
SENDGRID_ECHO_TO_STDOUT
will echo to stdout or any other file-like object that is passed to the backend via thestream
kwarg.SENDGRID_TRACK_EMAIL_OPENS
- defaults to true and tracks email open events via the Sendgrid service. These events are logged in the Statistics UI, Email Activity interface, and are reported by the Event Webhook.SENDGRID_TRACK_CLICKS_HTML
- defaults to true and, if enabled in your Sendgrid account, will tracks click events on links found in the HTML message sent.SENDGRID_TRACK_CLICKS_PLAIN
- defaults to true and, if enabled in your Sendgrid account, will tracks click events on links found in the plain text message sent.
Usage
Simple
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
Dynamic Template with JSON Data
First, create a dynamic template and copy the ID.
from django.core.mail import EmailMessage
msg = EmailMessage(
from_email='to@example.com',
to=['to@example.com'],
)
msg.template_id = "your-dynamic-template-id"
msg.dynamic_template_data = {
"title": foo
}
msg.send(fail_silently=False)
For more usage examples, see the tests.
Examples
- Marcelo Canina (@marcanuy) wrote a great article demonstrating how to integrate
django-sendgrid-v5
into your Django application on his site: https://simpleit.rocks/python/django/adding-email-to-django-the-easiest-way/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-sendgrid-v5-0.9.1.tar.gz
(10.7 kB
view hashes)
Built Distribution
Close
Hashes for django_sendgrid_v5-0.9.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 869fb75f9e216a58f94f37cfad6990c6691e8c79994f0d7287ad37412d6b717d |
|
MD5 | 59b789fd8c7045c5d8b76304c5770b09 |
|
BLAKE2b-256 | 41b2cf079a62b712fd1d0bb5139605ae10030182dd256f2dab5927733531d7ea |