This example demonstrates how to integrate Resend with Django using django-anymail, which provides a Django email backend for Resend. This approach uses Django's standard email API (send_mail(), EmailMessage, etc.)
To get the most out of this guide, you'll need to:
- Create an API key
- Verify your domain
- Install
virtualenvby runningpip install virtualenv
- Create and activate a new virtual env:
virtualenv venv
source venv/bin/activate- Install dependencies (includes django-anymail with Resend support):
pip install -r requirements.txt- Set your RESEND_API_KEY environment variable:
export RESEND_API_KEY="re_123456789"- Navigate to the Django project directory and run the development server:
cd resend_django_example
python manage.py runserver-
Test the email endpoints using curl:
Simple email (default recipient):
curl -X POST http://127.0.0.1:8000/send/
Simple email (custom recipient):
curl -X POST -d "[email protected]" http://127.0.0.1:8000/send/Template-based email (default recipient):
curl -X POST http://127.0.0.1:8000/template_send/
Template-based email (custom recipient):
curl -X POST -d "[email protected]" http://127.0.0.1:8000/template_send/
This example demonstrates three patterns for sending emails in Django:
Uses Django's send_mail() function - the simplest way to send emails in Django.
Shows how to use Django templates for email content with:
- Django's
render_to_string()for template rendering EmailMessageclass for more control- Resend-specific features like tags via
esp_extra
A reusable HTML email template that demonstrates:
- Template variables (user_name, user_email, etc.)
- Professional email styling
- Django template system integration
The django-anymail backend is configured in settings.py:
# Add anymail to INSTALLED_APPS
INSTALLED_APPS = [
# ...
'anymail',
]
# Configure Anymail with Resend backend
EMAIL_BACKEND = "anymail.backends.resend.EmailBackend"
ANYMAIL = {
"RESEND_API_KEY": os.environ.get("RESEND_API_KEY"),
}
DEFAULT_FROM_EMAIL = "[email protected]"- django-anymail Documentation
- django-anymail Resend Backend
- Django Email Documentation
- Resend Documentation
MIT License