June 8, 2020, 11:15 a.m.

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

Sometimes we have to use 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 G-mail's SMTP server to send the mails.
But there is a problem to use the g-mail SMTP server i.e due to security reasons google blocks our accounts if wwe send emails directly. So to keep it secure we have to enable SMTP service of our g-mail and we will generate 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 "invalid Password" error when try to authenticate.

You can use this link to enable 2-step verification. Enable 2-Step verification

(2.) Now you have generate an App Password. This will be used as your login password for login into SMTP server for this specific purpose.
Simply choose "other" when creating the app password and give it a name of your choice.

image not found
Step 2 Now we have to make some changes in our settings.py file.
#In settings.py

Note : If you use different mail service then check their port for outgoing mails 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 views.py file which will be used for sending mails 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"
   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.

Tips 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.”

Some recent posts

Complete process of changing database from SQLite to MySQL - How to migrat...

" How to download a file in django | Making a large file downloadable from ...

How to use proxy in python requests | http & https proxies - Scraping...

Top Django Querysets, How to filter records | Fetching data from database e...

How to change base url or domain in sitemap - Django ...

How to Make a Website - Everything you should know about web development...

What is Javascript - Features and usages ...

Top 5 Interview Questions : Tips for HR round ...

How to get job in IT : Perfect resume guide for IT job ...

Programming vs coding | difference between programming and coding ...

View all...