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.
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.
Now we have to make some changes in our settings.py file.
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.
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.
from django.core.mail import send_mail
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.
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.”
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
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
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
After reading this article you will never ever search for web development guide neither on Google nor on YouTube....- 2023-07-12
In this article we are talking about the most famous HR questions and the mind-set to answer those HR questions....- 2023-07-12
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