3 steps to send automatic emails in Django - How to send e-mails using python

Sometimes we have to use an automatic email service in our website for sending OTPs, Tokens, passwords or login credentials, etc. For this purpose, we use smtplib module of Python. To use this module in Django we have to just import the module and django.core.mail
Just make some changes in our code to send the emails automatically. We will use the Gmail SMTP server to send the emails.
But there is a problem to use the g-mail SMTP server i.e. due to security reasons google blocks our accounts if we send emails directly. So to keep it secure we have to enable SMTP service of our g-mail and we will generate a specific password for our G-mail account for this specific purpose.

Follow the easy and simple steps described below to send emails automatically in Django.

Step 1

First of all, let's enable our g-mail account's SMTP service securely.
(1.) The very first thing you will need to do is to ensure that you have 2-step verification enabled on your primary G-mail account. If you do not enable 2-step verification then you will get an "invalid Password" error when trying to authenticate.
You can use this link to enable 2-step verification. Enable 2-Step verification
(2.) Now you have to generate an App Password. This will be used as your login password for login into the SMTP server for this specific purpose.
Simply choose "other" when creating the app password and give it a name of your choice.

Step 2

Now we have to make some changes in our settings.py file.

#In settings.py
EMAIL_HOST_USER='[email protected]'

Note: If you use a different mail service then check their port for outgoing mail and check whether the server uses SSL or TLS. Thus, according to your email service, the above value may change. You can get these details of your SMTP service provider easily by googling the stuff.

Step 3

Write a function in the views.py file which will be used for sending emails automatically. Use the send_mail function and provide the required arguments to complete the process as shown below.

#In views.py
from django.core.mail import send_mail
def sendmail(request):
  receiver_list=[]   data={}
  subject="Email subject"
  message="Email body"
  sender="[email protected]"
   receiver="[email protected]"
   return render(request,'template.html',data)

subject − E-mail subject
message − E-mail body
sender − E-mail sender address
receiver − Receiver's e-mail address

Note: The receiver shown in the above code goes as a list in the function's argument. It means to send emails in bulk, you can pass all the mail addresses as a list where each address is a string.

recevier_list=['[email protected]','[email protected]']


1.) You can use


The "fail_silently=True" will raise an error/exception if mail is not sent. On the other hand if "fail_silently=False" will not stop the program whether the mail/mails is/are sent successfully or not.

2.) If you want to use your own domain mail id to send emails then you have to forward emails to Gmail and create an extra alias mail address using Gmail settings. Just go to settings, and click on “Accounts and Import.” Then click on “Add another email address you own.”

Recent Articles

Changing Database from SQLite to MySQL - Django

One of the main benefit to use django for web development is that it comes with an inbuilt database/filesystem i.e SQLITE. Managing database and files is super easy in django. But there are some drawbacks of using SQLITE...

  - 2023-07-12
How to download a file in django

Sometimes we have to serve a file which may be a large file from our project directory. Suppose you want to transfer a file from your project directory to your friend or to some other person/server/platform....

  - 2023-07-12
How to change base url or domain in sitemap - Django

A sitemap is the first important SEO step which elaborates a website's content to search engines. It provides information to search engines about the available content on our site, which helps them to crawl webpages for indexing....

  - 2023-07-12
How to Make a Website

After reading this article you will never ever search for web development guide neither on Google nor on YouTube....

  - 2023-07-12
What is Javascript - Features and usages

I'm kind of a person who always likes to get started by installing the things & writing some code. I love that but this time JavaScript requires a little bit of the theoretical basic so that we can understand what things we are learning, how hard and powerful it is and where they can be applied....

  - 2023-07-12
Top 5 Interview Questions : Tips for HR round

In this article we are talking about the most famous HR questions and the mind-set to answer those HR questions....

  - 2023-07-12
How to get job in IT - Perfect resume guide

In today's article I want to talk about how to prepare the best resume to get a job in IT. Yes, everybody wants to have a job and in order to get a job you have to apply for it and when you apply for that, the resume is one good thing that you always submit....

  - 2023-07-12