Sending Email (Django)
Django's email framework sends mail through send_mail() and EmailMessage, configured by EMAIL_BACKEND and SMTP settings. It is used for password resets, notifications, and contact-form delivery.
Django
Django wraps Python's email tools in a simple API. send_mail(subject, body, from_email, [recipients]) handles the common case, while the EmailMessage class adds attachments, HTML alternatives, CC/BCC, and custom headers for richer messages.
How mail is delivered depends on EMAIL_BACKEND. The SMTP backend sends through a real server (configured with EMAIL_HOST, EMAIL_PORT, credentials, and TLS settings), the console backend prints emails to the terminal during development, and the file backend writes them to disk for inspection.
Built-in features rely on this: the auth system's password-reset flow sends email through it. For production, point it at a transactional email provider, send mail asynchronously (via a task queue) so requests are not blocked, and never hard-code credentials — load them from the environment.
Syntax
from django.core.mail import send_mail
send_mail(subject, body, from_addr, [to])Examples
Python
from django.core.mail import send_mail
send_mail(
"Welcome",
"Thanks for signing up!",
"[email protected]",
["[email protected]"],
)
# In dev: EMAIL_BACKEND = "...console.EmailBackend" prints it.