Sending emails in Django is straightforward, thanks to its built-in email functionality. Whether you're notifying users, sending password resets, or automating communications, Django's send_mail function makes it easy to do so. In this guide, we'll walk through the process of setting up and sending emails in Django, then demonstrate how to test it using the Django shell.
Who Should Read This?
Developers who are already familiar with Django. This is not for absolute beginners, as it assumes familiarity with Django project structure and basic configuration.
Configure Email Settings in Django
To send emails with Django, you need to have a local Simple Mail Transfer Protocol (SMTP) server, or you need to access an external SMTP server, like your email service provider.
The following settings allow you to define the SMTP configuration to send emails with Django:
EMAIL_HOST: The SMTP server host; the default is localhost
EMAIL_PORT: The SMTP port; the default is 25
EMAIL_HOST_USER: The username for the SMTP server
EMAIL_HOST_PASSWORD: The password for the SMTP server
EMAIL_USE_TLS: Whether to use a Transport Layer Security (TLS) secure connection
EMAIL_USE_SSL: Whether to use an implicit TLS secure connection
For this blog, we will use Google's SMTP server. If you have a Gmail account, edit the settings.py file of your project and add the following code:
# Email server configuration
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_account@gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Replace your_account@gmail.com with your actual Gmail account.
N/b If you can’t use an SMTP server, you can tell Django to write emails to the console by adding the following settings to the settings.py file:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Steps to Configure Gmail SMTP
Gmail requires secure settings and an App Password for authentication. Google allows you to create app-specific passwords for your account. An app password is a 16-digit passcode that grants less secure apps or devices permission to access your Google account.
Open https://myaccount.google.com/ in your browser. On the left menu, click on Security. You will See the following screen:
Click on the 2-Step Verification. When you click 2-step verification, you will see the following screen:
Click on 'app passwords,' enter a name for your app, and then click 'Create.'
A new password will be generated like this:
Copy the generated app password.
Edit the settings.py file of your project and add the app password to the EMAIL_HOST_PASSWORD setting, as follows:
# Email server configuration
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_account@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxxxxx'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Open the Python shell by running the following command:
python manage.py shell
Run the following code in the python shell:
Python 3.12.3 (main, Jun 18 2025, 17:59:45) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.mail import send_mail
>>> send_mail('Django mail', 'Hey there, i hope this email finds you well', 'stephenkihuni55@gmail.com',
... ['stephenkihuni55@gmail.com'], fail_silently=False)
The send_mail() function takes the subject, message, sender, and list of recipients as required arguments. By setting the optional argument fail_silently=False, we are telling it to raise an exception if the email cannot be sent. If the output you see is 1, then your email was successfully sent.
Check your inbox. You should have received the email:
Congrats, you just sent your first email with Django!
Thank you for reading all the way through, and if you enjoyed it, please leave a comment below.
Top comments (0)