How to Send Emails in Django 6.1.1
Django 6.1 introduces the biggest email configuration change in years. Previous Django versions used EMAIL_BACKEND together with several EMAIL_* settings. Starting with Django 6.1, these settings are deprecated in favor of the new MAILERS configuration.
Your existing project will continue to work for now, but Django will emit deprecation warnings. The old settings are scheduled for removal in a future Django release, so new projects should use MAILERS immediately, and existing projects should migrate as soon as possible.
What Changed in Django 6.1?
Before Django 6.1, email configuration looked like this:
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_HOST_USER = "your@gmail.com"
EMAIL_HOST_PASSWORD = "your-app-password"
EMAIL_USE_TLS = True
Starting with Django 6.1, all of these settings are replaced by a single MAILERS dictionary.
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.smtp.EmailBackend",
"OPTIONS": {
"host": "smtp.gmail.com",
"port": 587,
"username": "your@gmail.com",
"password": "your-app-password",
"use_tls": True,
},
},
}
Instead of configuring multiple global variables, every email backend is now defined as a named mailer. This makes it possible to configure multiple email providers inside a single Django project.
Old Configuration vs New Configuration
| Old Setting | New MAILERS Equivalent |
|---|---|
EMAIL_BACKEND |
BACKEND |
EMAIL_HOST |
OPTIONS["host"] |
EMAIL_PORT |
OPTIONS["port"] |
EMAIL_HOST_USER |
OPTIONS["username"] |
EMAIL_HOST_PASSWORD |
OPTIONS["password"] |
EMAIL_USE_TLS |
OPTIONS["use_tls"] |
EMAIL_USE_SSL |
OPTIONS["use_ssl"] |
EMAIL_TIMEOUT |
OPTIONS["timeout"] |
EMAIL_SSL_CERTFILE |
OPTIONS["ssl_certfile"] |
EMAIL_SSL_KEYFILE |
OPTIONS["ssl_keyfile"] |
EMAIL_FILE_PATH |
OPTIONS["file_path"] |
Every deprecated EMAIL_* setting now lives inside the OPTIONS dictionary of a mailer configuration.
Easy Migration Guide
Before (Django 6.0 and Earlier)
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_HOST_USER = "your@gmail.com"
EMAIL_HOST_PASSWORD = "password"
EMAIL_USE_TLS = True
After (Django 6.1+)
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.smtp.EmailBackend",
"OPTIONS": {
"host": "smtp.gmail.com",
"port": 587,
"username": "your@gmail.com",
"password": "password",
"use_tls": True,
},
},
}
For most projects, this is the only settings change required.
Sending an Email
The actual email sending API has not changed if you only use the default mailer.
from django.core.mail import send_mail
send_mail(
subject="Welcome",
message="Thanks for registering!",
from_email="[email protected]",
recipient_list=["[email protected]"],
)
Using Multiple Email Providers
The primary reason Django introduced MAILERS is to support multiple named email backends.
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.smtp.EmailBackend",
"OPTIONS": {
"host": "smtp.gmail.com",
"port": 587,
"username": "[email protected]",
"password": "password",
"use_tls": True,
},
},
"support": {
"BACKEND": "django.core.mail.backends.smtp.EmailBackend",
"OPTIONS": {
"host": "smtp.office365.com",
"port": 587,
"username": "[email protected]",
"password": "another-password",
"use_tls": True,
},
},
}
You can then choose which mailer to use when sending email.
from django.core.mail import send_mail
send_mail(
subject="Support Ticket",
message="Your ticket has been received.",
from_email="[email protected]",
recipient_list=["[email protected]"],
using="support",
)
The new using argument replaces the older connection argument, which is now deprecated.
Development Email Backend
Instead of SMTP, you can print emails directly in the terminal while developing.
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.console.EmailBackend",
},
}
Every email will be displayed in your terminal instead of being sent.
File Email Backend
To save emails into files instead of sending them:
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.filebased.EmailBackend",
"OPTIONS": {
"file_path": BASE_DIR / "sent_emails",
},
},
}
In-Memory Backend (Testing)
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.locmem.EmailBackend",
},
}
Django's test runner automatically switches to the in-memory backend during tests, preventing real emails from being sent.
Other Email Changes in Django 6.1
EMAIL_BACKENDis deprecated.- All SMTP-related
EMAIL_*settings are deprecated. mail.get_connection()is deprecated.- The
connection=parameter has been replaced byusing=. auth_userandauth_passwordparameters are deprecated.- Direct creation of
EmailBackendobjects is discouraged. - The new
django.core.mail.mailersregistry manages configured mailers.
These changes are part of Django's long-term transition to a more flexible email configuration system. Existing code still works for compatibility, but projects should migrate before the deprecated APIs are removed.
Complete Gmail Example
MAILERS = {
"default": {
"BACKEND": "django.core.mail.backends.smtp.EmailBackend",
"OPTIONS": {
"host": "smtp.gmail.com",
"port": 587,
"username": "[email protected]",
"password": "YOUR_GMAIL_APP_PASSWORD",
"use_tls": True,
"timeout": 30,
},
},
}
DEFAULT_FROM_EMAIL = "[email protected]"
For Gmail, use an App Password instead of your Google account password if two-factor authentication is enabled.
Summary
- Django 6.1 replaces
EMAIL_BACKENDand related settings withMAILERS. - Existing projects continue to work temporarily but receive deprecation warnings.
- The new configuration allows multiple named email backends.
- The
usingargument replacesconnectionwhen selecting a mailer. - New Django projects should adopt
MAILERSimmediately.
No comments yet. Be the first to comment!