How to send email with Django: Complete SMTP guide
Source: Dev.to
Understanding Django’s email system
Django provides a robust email‑sending framework through its django.core.mail module. This module includes functions and classes for sending basic, HTML, and mass emails.
The module requires a backend to function properly, so you’ll need to configure the email backend you want to use for your project.

Django supports many out‑of‑the‑box email backends. The SMTP Backend is the most popular and works well for most applications.
In addition to the default backends, you can build a custom email backend, but this guide focuses on the built‑in SMTP Email Backend.
How to send email with Django
Prerequisites
- Django installed in your project (the guide uses Django 5.0 but works with Django 3.x and above)
- Basic knowledge of Django and Python
- A working Django project
(see the official Django installation guide if you need to create one) - An email service provider (the examples use SendLayer, but any SMTP provider will work)
If you choose SendLayer, you can start with a free account that allows up to 200 emails.
Start your free trial at SendLayer
After creating an account, you’ll need to authorize your sending domain, which improves deliverability and verifies your account.
Configuring your email backend
Open your project’s settings.py and add the following configuration (replace the placeholder values with your own SMTP credentials):
# Django email configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendlayer.net' # Replace with your SMTP server
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-sendlayer-username'
EMAIL_HOST_PASSWORD = 'your-sendlayer-password'

Retrieve your SMTP credentials from your SendLayer account (or from whichever provider you use) and replace the placeholders accordingly.
Securing your credentials
Storing usernames and passwords directly in code is insecure. Use environment variables instead.
-
Create a
.envfile in the project root:EMAIL_HOST_USER='your-sendlayer-username' EMAIL_HOST_PASSWORD='your-sendlayer-password' -
Install
python-decouple:pip install python-decouple -
Update
settings.pyto read the variables:from decouple import config EMAIL_HOST_USER = config('EMAIL_HOST_USER') EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
Method 1: Send plain email using send_mail() function
send_mail() is a built‑in shortcut for sending simple text emails.
Required parameters
subject: Email subject linemessage: Plain‑text bodyfrom_email: Sender addressrecipient_list: List of recipient addresses
Optional parameters
fail_silently:Falseraises exceptions;Truesuppresses themconnection: Custom email backend (defaults to the one defined in settings)html_message: HTML version of the email body
Example
from django.core.mail import send_mail
send_mail(
subject='Welcome to Our App',
message='Thank you for signing up!',
from_email='paulie@example.com',
recipient_list=['user@example.com'],
fail_silently=False,
)
Pro Tip: When using SendLayer, the
from_emailshould belong to the domain you have authorized (e.g.,@example.com).
Sending to multiple recipients
from django.core.mail import send_mail
send_mail(
subject='Welcome to Our App',
message='Thank you for signing up!',
from_email='paulie@example.com',
recipient_list=[
'user1@example.com',
'user2@example.com',
'user3@example.com',
],
fail_silently=False,
)
Method 2: Send email with EmailMessage class
The EmailMessage class offers more flexibility than send_mail(), allowing you to add attachments, set custom headers, and send HTML content.
