Skip to main content

An implementation of Django's EmailBackend compatible with sendgrid-python v5+

Project description

django-sendgrid-v5

Latest Release

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

  1. pip install django-sendgrid-v5
  2. In your project's settings.py script:
    1. Set EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
    2. 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"]

Other settings

  1. To toggle sandbox mode (when django is running in DEBUG mode), set SENDGRID_SANDBOX_MODE_IN_DEBUG = True/False.
    1. 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.
  2. SENDGRID_ECHO_TO_STDOUT will echo to stdout or any other file-like object that is passed to the backend via the stream kwarg.
  3. 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.
  4. 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.
  5. 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)

The kitchen sink EmailMessage (all of the supported sendgrid-specific properties)

from django.core.mail import EmailMessage

msg = EmailMessage(
  from_email='to@example.com',
  to=['to@example.com'],
  cc=['cc@example.com'],
  bcc=['bcc@example.com'],
)

# Personalization custom args
# https://sendgrid.com/docs/for-developers/sending-email/personalizations/
msg.custom_args = {'arg1': 'value1', 'arg2': 'value2'}

# Reply to email address (sendgrid only supports 1 reply-to email address)
msg.reply_to = 'reply-to@example.com'

# Send at (accepts an integer per the sendgrid docs)
# https://docs.sendgrid.com/for-developers/sending-email/scheduling-parameters#send-at
msg.send_at = 1600188812

# Transactional templates
# https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/
msg.template_id = "your-dynamic-template-id"
msg.dynamic_template_data = {  # Sendgrid v6+ only
  "title": foo
}
msg.substitutions = {
  "title": bar
}

# Unsubscribe groups
# https://sendgrid.com/docs/ui/sending-email/unsubscribe-groups/
msg.asm = {'group_id': 123, 'groups_to_display': ['group1', 'group2']}

# Categories
# https://sendgrid.com/docs/glossary/categories/
msg.categories = ['category1', 'category2']

# IP Pools
# https://sendgrid.com/docs/ui/account-and-settings/ip-pools/
msg.ip_pool_name = 'my-ip-pool'


msg.send(fail_silently=False)

FAQ

How to change a Sender's Name ?

from_email="John Smith <john.smith@example.org>" You can just include the name in the from_email field of the EmailMessage class

msg = EmailMessage(
  from_email='Sender Name <from@example.com>',
  to=['to@example.com'],
)

How to make mails to multiple users private (hide all the email addresses to which the mail is sent) to each person (personalization) ?

Setting the make_private attribute to True will help us achieve it

msg = EmailMessage(
  from_email='Sender Name <from@example.com>',
  to=['to@example.com','abc@example.com','xyz@example.com'],
)
msg.make_private = True

Examples

Stargazers over time

Stargazers over time

Project details


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-1.2.3.tar.gz (15.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

django_sendgrid_v5-1.2.3-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

django_sendgrid_v5-1.2.3-py2.py3-none-any.whl (11.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-sendgrid-v5-1.2.3.tar.gz.

File metadata

  • Download URL: django-sendgrid-v5-1.2.3.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.9.6 requests/2.31.0 setuptools/65.5.0 requests-toolbelt/1.0.0 tqdm/4.66.1 CPython/3.11.5

File hashes

Hashes for django-sendgrid-v5-1.2.3.tar.gz
Algorithm Hash digest
SHA256 3887aafbb10d5b808efc2c1031dcd96fd357d542eb5affe38fef07cc0f3cfae9
MD5 66ac01337785da82821d7f8e40b85176
BLAKE2b-256 946f3df963ca11064379c0a1f4398c5f083802c52eb7b1d42bde0615462bd8d3

See more details on using hashes here.

File details

Details for the file django_sendgrid_v5-1.2.3-py3-none-any.whl.

File metadata

  • Download URL: django_sendgrid_v5-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.11.2 requests/2.32.3 setuptools/65.5.0 requests-toolbelt/1.0.0 tqdm/4.67.1 CPython/3.11.5

File hashes

Hashes for django_sendgrid_v5-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f6a44ee37c1c3cc7d683a43c55ead530417be1849a8a41bde02b158009559d9d
MD5 e25e16fcb08974c7075ceac9ffa97adc
BLAKE2b-256 4c79f1dcd20e88f78036f700b729a60a116f5c96f624ed2004e3d351a8f936e6

See more details on using hashes here.

File details

Details for the file django_sendgrid_v5-1.2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: django_sendgrid_v5-1.2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.9.6 requests/2.31.0 setuptools/65.5.0 requests-toolbelt/1.0.0 tqdm/4.66.1 CPython/3.11.5

File hashes

Hashes for django_sendgrid_v5-1.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2d2fa8a085d21c95e5f97fc60b61f199ccc57a27df8da90cd3f29a5702346dc6
MD5 856c2d45ea616d48528654c967404225
BLAKE2b-256 f72c742986e2633d7ea9aa3dc5523a73fe0cb938f0d9df066319e2193bdd3973

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page