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
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.
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.
- subject is the subject 😁
- message is the actual message
- 3rd parameter is from_email if it's None Django will use the value of the DEFAULT_FROM_EMAIL setting which we have defined in the settings.py already. If it's not None you need to also provide another arguments see here.
- recipient list is the list of email addresses you want to send the email to
- fail_silently will raise an error if an error occurred see here. This parameter is optional it's default value is True. Set this to false because this will help us know whether the email is sent successfully or not .
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"
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 😘.