django + email
django + email

how to send emails with django using gmail

sriram
sriram
June 24

In this post I will quickly tell you how you can use Gmail to send email😉 with 3 simple steps. It takes only 5 minutes to setup. What you all need is a gmail account.  The current Gmail email sending limit is 2000 emails per 24 hours that's huge you really don't want to miss this resource.

I will show you two methods for sending email. The first one is quick to set up but I really don't recommend this method for security reasons. The second method just takes 3 minutes more than the first.

So let's get started.

The following method uses your actual google account password that is the password you use for logging in to your google account so only I told this method is not recommended. There are only small changes between these 2 methods ( only changes to variable value and generating an app password ). Procedure to send mail in views.py is going to be same for both. So let's see the method 1. 

1. Configuring your google account to send mail

Go to your google account and to security  under Less secure app access enable less secure apps: ON

your google account > security > enable less secure app access

Google allows to use your google account in your apps in our case it's django application only when this setting is enables. Now let's see what to do in django.

2. Configuring settings in django

Go to your django project's settings file and create these following variables at the end of your settings file.

EMAIL_USE_TLS refers whether to use TLS ( Transport Layer Security ) or not. According to google Gmail always uses TLS by default.  

EMAIL_POST 587  is the default mail submission port

EMAIL_HOST_USER is your gmail address 

EMAIL_HOST_PASSWORD is the password for your gmail account.

keep this password so secure
This is your google password. for example if you push your code to github public repo then you should this password definitely safe.

That's it all configuration finished let's see how to programmatically send email using django's send_mail function

3. Sending email 

send_mail function takes 4 required parameters and 5 optional parameters let's see what are these parameters one by one. here you can find all parameters this function takes.

Method 2 using an app password :

To send email without using your actual google account password you need an app password. To generate this go to your google account and enable 2-step verification. 2-step verification is must to get an app password. After enabling 2-step verification go to app passwords under singing in to google under security.

your google account > security > signing in to google > app passwords

Now generate an app password by giving a custom name and copy the app password and assign this value to the variable EMAIL_HOST_PASSWORD.

EMAIL_HOST_PASSWORD = "your generated app password"

Google app password
Google app password
Again keep this app password secure
People can access your google account using this password. 

That's it all done now you can send email to your users or your loved ones or to anyone. Hope you liked this post. Tell your thoughts in the comments section I would love to hear from you guys. follow me for more cool posts like this. Be sure to like this post that's what motivates me.

If you'd like to support my work, click the three dots on the top right and the coin symbol or here. This help will help us improve this website. By the way I am the founder and CEO of this website 🤨.

Stay happy. Stay safe my buddie's 😃 luv you 😘.  


Today's deal from platiplus

More from the author

django + email

how to send emails with django using gmail

Comments (2)

Add a Comment

Reply