django-ses-backend
Django AWS SES (Amazon Simple Email Service) email backend.
Features
- Send emails via AWS SES without needing
boto3or any other AWS SDK. - No SMTP configuration required and faster email sending.
- Supports sending plain text and HTML emails, including multi-part messages.
- Lightweight and easy to integrate.
- Supports Django's
EmailMessageandEmailMultiAlternatives. - Handles
Reply-Toand custom headers. - Custom SES client implementation with AWS signature v4, rate-limit retries, and exponential backoff.
- Context manager support for use with
with SESEmailBackend() as connection:.
Requirements
- Python 3.11+
- Django 4.2+
Installation
Install the package using pip:
pip install django-ses-backend
AWS Setup
Step 1: Create an AWS SES Account
- Sign in to the AWS Management Console.
- Navigate to Amazon SES service.
- Verify your email address or domain in the Verified Identities section.
- Move your SES account out of Sandbox Mode (if needed) by requesting production access.
Step 2: Create an IAM User for SES
- Go to IAM in the AWS Console.
- Create a new user and enable Programmatic access.
- Attach the policy AmazonSESFullAccess (or a custom policy with
ses:SendEmailpermissions). - Save the Access Key ID and Secret Access Key.
Configuration
Update your Django settings:
# settings.py
EMAIL_BACKEND = 'django_ses_backend.backends.SESEmailBackend'
SES_AWS_ACCESS_KEY_ID = 'YOUR_AWS_ACCESS_KEY_ID'
SES_AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET_ACCESS_KEY'
SES_AWS_REGION = 'YOUR_AWS_REGION'
# Optional advanced settings
SES_ENDPOINT_URL = None # Custom endpoint URL
SES_ENDPOINT_PATH = None # Custom endpoint path
SES_TIMEOUT = 10 # HTTP request timeout in seconds
SES_MAX_RETRIES = 3 # Max retry attempts for rate-limits/server errors
SES_RETRY_DELAY = 1.0 # Base delay (seconds) for exponential backoff
Usage
You can send emails using Django's built-in send_mail or EmailMessage:
from django.core.mail import send_mail
send_mail(
subject="Hello from AWS SES",
message="This is a test email.",
from_email="your-email@example.com",
recipient_list=["recipient@example.com"],
)
Or using EmailMessage for more customization:
from django.core.mail import EmailMessage
email = EmailMessage(
subject="Hello from AWS SES",
body="This is a test email.",
from_email="your-email@example.com",
to=["recipient@example.com"],
)
email.send()
Sending HTML Emails
Supports both HTML and text content:
from django.core.mail import EmailMultiAlternatives
email = EmailMultiAlternatives(
subject="HTML Email Test",
body="Fallback plain text content",
from_email="your-email@example.com",
to=["recipient@example.com"],
)
email.attach_alternative("<h1>Hello from AWS SES</h1>", "text/html")
email.send()
Handling Reply-To and Custom Headers
email = EmailMessage(
subject="Email with Reply-To",
body="Hello",
from_email="your-email@example.com",
to=["recipient@example.com"],
reply_to=["replyto@example.com"],
headers={"Message-ID": "<custom.id@example.com>"},
)
email.send()
Error Handling
SESClientErroris raised for generic sending failures.SESRatelimitErroris raised when AWS rate-limits are hit.- Retries are performed automatically with exponential backoff for 429/5xx errors.
try:
email.send()
except SESClientError as e:
print(f"Failed to send email: {e}")
Logging
Enable logging to track email sending:
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("django_ses_backend")
Notes
- Always include both text and HTML versions for best deliverability.
- Attachments are ignored by this backend.
- Ensure your AWS SES account is verified and out of sandbox mode.
- Configure AWS IAM policies to grant
ses:SendEmailpermissions.
Contributing
Feel free to submit issues or pull requests on GitHub to improve this package.
Release files for django-ses-backend 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_ses_backend-0.1.3.tar.gz | 6.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_ses_backend-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.5 kB
Release files / django_ses_backend-0.1.3.tar.gz
| Download URL | django_ses_backend-0.1.3.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a01e203db46f8429f6692f7025d2b1816187c6f0682175182f350e2959bb2f7
|
|
BLAKE2b-256 checksum How to use checksums |
230298d6921848ec6ab42621fecc87b6ef6ffc806218029bbf1eb54cb79cfbe9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.13.7 Darwin/24.6.0
|
Release files / django_ses_backend-0.1.3-py3-none-any.whl
| Download URL | django_ses_backend-0.1.3-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b27cabbbc97741bfea49a6ed41d504ca835f8b682dda93c57a5f104dd065e1c
|
|
BLAKE2b-256 checksum How to use checksums |
c2660edd99568c96acfe5e0640f32bb0f79bb6f5d53bde000874e5a24eef54a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.13.7 Darwin/24.6.0
|