Django email backend implementation compatible with SendGrid API v5+ for seamless email delivery integration
npx @tessl/cli install tessl/pypi-django-sendgrid-v5@1.3.0A Django email backend implementation that integrates with SendGrid's REST API v5+ for reliable email delivery. This package provides seamless email sending functionality through Django's standard email API while offering advanced SendGrid-specific features like dynamic templates, personalization, webhook signature verification, tracking, and scheduling.
pip install django-sendgrid-v5from sendgrid_backend import SendgridBackend, __version__For Django settings integration:
# In settings.py
EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
SENDGRID_API_KEY = "your-sendgrid-api-key"from django.core.mail import send_mail, EmailMessage
# Simple email sending using Django's standard API
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
# Enhanced email with SendGrid-specific features
msg = EmailMessage(
subject='Advanced Email',
body='Email content',
from_email='sender@example.com',
to=['recipient@example.com'],
)
# Add SendGrid-specific attributes
msg.template_id = "d-abc123def456"
msg.dynamic_template_data = {"name": "John", "product": "Widget"}
msg.categories = ["marketing", "newsletter"]
msg.send()The package extends Django's BaseEmailBackend with SendGrid API integration:
The core SendGrid email backend implementation that replaces Django's default email backend with SendGrid API integration.
class SendgridBackend(BaseEmailBackend):
def __init__(self, *args, **kwargs): ...
def send_messages(self, email_messages) -> int: ...
def echo_to_output_stream(self, email_messages): ...Dynamic template support with variable substitution and personalized content for individual recipients.
# EmailMessage extensions for templates
msg.template_id = "template-id"
msg.dynamic_template_data = dict # v6+
msg.substitutions = dict # v5
msg.personalizations = list # Advanced personalizationDjango settings integration for API keys, tracking options, sandbox mode, and other SendGrid-specific configurations.
# Django settings
SENDGRID_API_KEY = str
SENDGRID_SANDBOX_MODE_IN_DEBUG = bool
SENDGRID_TRACK_EMAIL_OPENS = bool
SENDGRID_TRACK_CLICKS_HTML = bool
SENDGRID_TRACK_CLICKS_PLAIN = boolSecure webhook handling with signature verification for processing SendGrid delivery events and email tracking data.
@verify_sendgrid_webhook_signature
def webhook_handler(request): ...
def check_sendgrid_signature(request) -> bool: ...Django signal system for handling email delivery events, enabling custom logging, analytics, and error handling.
sendgrid_email_sent = django.dispatch.Signal()
# Signal arguments: sender, message, fail_flag# Package version
__version__: str # Package version string (e.g., "1.3.0")
# Email message extensions
class EmailMessage:
# Template support
template_id: str
dynamic_template_data: dict # v6+
substitutions: dict # v5
# Personalization
personalizations: list
make_private: bool
custom_args: dict
# Scheduling and delivery
send_at: int # Unix timestamp
ip_pool_name: str
# Organization and tracking
categories: list[str]
asm: dict # Unsubscribe groups
reply_to_list: list[str] # v6+
# Advanced settings
mail_settings: MailSettings
tracking_settings: TrackingSettings