Guide · Python
How to send email with Django
Configure Django's built-in email backend to send through Stack Emails over SMTP, so send_mail, password-reset emails and admin errors just work.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
export STACKEMAILS_API_KEY=se_live_xxxxxxxxxxxx
2. Send your first email
import os
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.stackemails.com"
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = "stackemails"
EMAIL_HOST_PASSWORD = os.environ["STACKEMAILS_API_KEY"]
DEFAULT_FROM_EMAIL = "Acme <hello@mail.acme.com>"
SERVER_EMAIL = "alerts@mail.acme.com"
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
Send a message
from django.core.mail import send_mail
send_mail(
subject="Welcome to Acme",
message="Thanks for signing up!",
from_email=None, # uses DEFAULT_FROM_EMAIL
recipient_list=["ada@example.com"],
html_message="<p>Thanks for signing up!</p>",
)
Django's password reset views use the same backend automatically. Use port 587 with EMAIL_USE_TLS = True instead of 465 + EMAIL_USE_SSL if you prefer STARTTLS; don't set both.
Calling the API instead
For scheduling, tags or idempotency keys, call the HTTP API with requests as shown in the Python guide, ideally from a Celery task.
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.