My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to send email on django app using SMTP server

Deactivated User's photo
Deactivated User
·Sep 21, 2020·

3 min read

Hi, I decided to share these steps on how to send email on a django app while using Django framework because personally I had a lot of difficulties sending email in a django app using google SMTP server.

This is used to send email to users, usually to reset password or any other mail. Most importantly to ensure the password of the developer stays hidden.

To begin with : This is done using Google’s Gmail SMTP server. Gmail SMTP server is a free SMTP service which anyone who has a Gmail account can use to send emails. You can use it with personal emails, or even with your website if you are sending emails for things such as contact forms, newsletter blasts, or notifications. To read more check ... siteground.com/kb/google_free_smtp_server

Firstly, in your app settings.py add this code:

(add at the top)

 import os

(add the following at the bottom)

  EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

  EMAIL_HOST = 'smtp.gmail.com'

  EMAIL_PORT = 587

  EMAIL_USE_TLS = True

Next step, turn off your goggle Two-Step Verification.

In case, you don't know to... follow this link. myaccount.google.com/u/1/signinoptions/two-..

Next,

  1. On your browser search ' app password ' , click on the second site shown.

  2. On the select app menu, select other and enter 'Gmail'. Then, select device.

  3. Lastly, generate password and copy the generated password.

Next step, open up a new file in a separate window. Example 'env.py'. Add the following codes.

 import os


email_user = '(enter email address)'

email_password = '(enter generated password)'


print(email_user)


print(email_password)

Next step, set up environment variable. To do this, for window users. search control panel on your device.

  • Click on System and security.

  • Click on system > advanced system settings.

  • Click environment variable

  • In user variable click new

app 2.PNG

  • As variable name enter 'EMAIL_USER'

  • As variable value put in your email address.

  • Click new again.

Repeat the steps above but this time use EMAIL_PASSWORD and as variable value put in the generated password. Click 'okay' and return to code ie. 'env.py', edit the code above and replace with the following.

 import os


email_user = os.environ.get('EMAIL_USER')

email_password = os.environ.get('EMAIL_PASSWORD')

print(email_user)

print(email_password)

Run this code, on your terminal the email address and password would be displayed. If this doesn't happen kindly make sure you go through the steps again carefully.

Lastly, back to your django settings.py code add the following code at the bottom.

email_user = os.environ.get(EMAIL_USER)

email_password = os.environ.get(EMAIL_PASSWORD)

FINAL PART.PNG

With this steps a mail will be sent to the selected email address.

Conclusion

Once this is done accordingly email can be sent through a simple line of codes without the password of the developer getting jeoparadized. I hope this was helpfully and thank you for stopping by.