Django How to Send Email to Gmail Account

In this Django article i want to show you How to Send Email to Gmail Account, basically sending emails with Django is easy and straightforward. the most important thing is to have a local SMTP server or define the configuration of an external SMTP server.

 

Also you can check

1: How To Build News Application In Django

 

OK now first of all you need to create a new django project using this command. i have called the project MyProject

 

 

First of all you need to change directory to the created project and after that you need to create an App, i have called the app MyApp.

 

 

So now you need to add some configuration of SMTP server, open your settings.py file and add these configurations.

 

OK in the above code iam using GMAIL SMTP Server, but if you have your own you can change that. also iam telling to django that if my django project is not in debug mode, use the real SMTP Server, but if it is in debug mode you can use django backend console for email.

 

  • EMAIL_BACKEND: This is the backend for the email
  • EMAIL_HOST_USER: Username for the SMTP server
  • EMAIL_HOST: The SMTP server host; the default is localhost
  • EMAIL_PORT: The SMTP port; the default is 25
  • EMAIL_HOST_PASSWORD: Password for the SMTP server
  • EMAIL_USE_TLS: Whether to use a TLS secure connection
  • EMAIL_PASSWORD: This is the password

 

 

After this you need to open django shell using this command .

 

 

Now you need to import send_mail() method form django.core.mail.

 

 

After this write this code for sending email.

 

The send_mail() function takes the subject, message, sender, and list of
recipients as required arguments. By setting the optional argument
fail_silently=False, we are telling it to raise an exception if the email
couldn’t be sent correctly. If the output you see is 1, then your email
was successfully sent.

 

Note: If you are sending emails by Gmail with the preceding
configuration, you might have to enable access for less secured apps, you can check this link for more information

https://support.google.com/accounts/answer/6010255?hl=en

also of you have two step verification for your gmail account, you can not enable less secure apps in gmail.

 

 

After runing the code you these the are outputs for the console and also gmail.

 

Django How to Send Email to Gmail Account
Django How to Send Email to Gmail Account

 

 

Python Django Send Email
Python Django Send Email

 

 

 

Also you can watch the complete video for this article

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×