Django email backend for AWS SES (Amazon Simple Email Service) without boto3
Project description
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_ses_backend-0.1.3.tar.gz.
File metadata
- Download URL: django_ses_backend-0.1.3.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.7 Darwin/24.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a01e203db46f8429f6692f7025d2b1816187c6f0682175182f350e2959bb2f7
|
|
| MD5 |
8059291784720cdc2da076abc6454ff1
|
|
| BLAKE2b-256 |
230298d6921848ec6ab42621fecc87b6ef6ffc806218029bbf1eb54cb79cfbe9
|
File details
Details for the file django_ses_backend-0.1.3-py3-none-any.whl.
File metadata
- Download URL: django_ses_backend-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.7 Darwin/24.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b27cabbbc97741bfea49a6ed41d504ca835f8b682dda93c57a5f104dd065e1c
|
|
| MD5 |
e3a8af486bcb3e1df0f9fdbf30654ec5
|
|
| BLAKE2b-256 |
c2660edd99568c96acfe5e0640f32bb0f79bb6f5d53bde000874e5a24eef54a3
|