Send emails using Django with SMTP
Prerequisites
Section titled “Prerequisites”Before you start, you'll need:
- A Resend API key
- A verified domain
- Install
virtualenvby runningpip install virtualenv
Setup your environment
Create and activate your new virtualenv.
Bash virtualenv venv source venv/bin/activateInstall dependencies.
Shell pip install -r requirements.txtSet your
RESEND_API_KEYenvironment variable by running.Shell export RESEND_API_KEY="re_xxxxxxxxx"Send email using Django's SMTP EmailMessage
Set the necessary attributes in your
settings.pyfile.Python EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' RESEND_SMTP_PORT = 587 RESEND_SMTP_USERNAME = 'resend' RESEND_SMTP_HOST = 'smtp.resend.com'Use Django's
get_connectionandEmailMessagePython import os from django.conf import settings from django.http import JsonResponse from django.core.mail import EmailMessage, get_connection # Sample Django view def index(request): subject = "Hello from Django SMTP" recipient_list = ["delivered@resend.dev"] from_email = "onboarding@resend.dev" message = "<strong>it works!</strong>" with get_connection( host=settings.RESEND_SMTP_HOST, port=settings.RESEND_SMTP_PORT, username=settings.RESEND_SMTP_USERNAME, password=os.environ["RESEND_API_KEY"], use_tls=True, ) as connection: r = EmailMessage( subject=subject, body=message, to=recipient_list, from_email=from_email, connection=connection).send() return JsonResponse({"status": "ok"})
Examples
Section titled “Examples”Django SMTP Example
See the full source code.