
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
django-sendgrid-v5
Advanced tools
An implementation of Django's EmailBackend compatible with sendgrid-python 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.
pip install django-sendgrid-v5EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"SENDGRID_API_KEY = os.environ["SENDGRID_API_KEY"]SENDGRID_SANDBOX_MODE_IN_DEBUG = True/False.
SENDGRID_ECHO_TO_STDOUT will echo to stdout or any other file-like
object that is passed to the backend via the stream 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.SENDGRID_HOST_URL - Allows changing the base API URI. Set to https://api.eu.sendgrid.com to use the EU region.from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
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)
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)
Version 6 of the sendgrid package or later includes some helper functions to
cryptographically verify the signature and contents of events from the Sendgrid
Events webhook.
This project includes some additional helpers for Sendgrid's webhook signature verification.
settings.py and set SENDGRID_WEBHOOK_VERIFICATION_KEY to your verification key value.import json
from datetime import datetime
from django.db import transaction
from django.http import HttpRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from post_office.models import Email, Log as EmailLog, STATUS
from pytz import utc
from sendgrid_backend.decorators import verify_sendgrid_webhook_signature
EVENTS = {'delivered': STATUS.sent, 'bounce': STATUS.failed, 'blocked': STATUS.failed}
@csrf_exempt
@require_POST
@verify_sendgrid_webhook_signature
def sendgrid_deliverability_webhook_handler(request: HttpRequest) -> HttpResponse:
"""
Example webhook handler to save delivered, bounce, and blocked events to
the email log.
"""
for msg_dict in reversed(json.loads(request.body)):
if event := EVENTS.get(msg_dict.get('event', None), None):
event_timestamp = datetime.fromtimestamp(msg_dict.get('timestamp'), tz=utc)
with transaction.atomic():
Email.objects.filter(message_id=msg_dict.get('smtp-id', None)).update(
last_updated=event_timestamp,
status=event,
)
EmailLog.objects.create(
email__message_id=msg_dict.get('smtp-id', None),
date=event_timestamp,
status=event,
message=json.dumps(msg_dict),
)
return HttpResponse("ok")
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
django-sendgrid-v5 into your Django application on his site: https://simpleit.rocks/python/django/adding-email-to-django-the-easiest-way/django-sendgrid-v5 to make a contact form for your web application: https://rx-36.life/create-a-contact-form-using-sendgrid-with-django/FAQs
An implementation of Django's EmailBackend compatible with sendgrid-python v5+
We found that django-sendgrid-v5 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.