TIL/2020–11–15

Today I learned about: Django’s send_mail function, django-dotenv, and how to use Mailgun Part 1

This is in continuation of my Airbnb clone project. I am trying to use Mailgun (Email Service/API that lets users send, receive and track email) as a means to send a verification email for the user who is signing up with his/her email.

First of all I had to sign up in Mailgun to be able to use their features. In the Django Documentation regarding send_mail and how to use it, we need to configure my config settings.py and have the following:

EMAIL_HOST = “”

EMAIL_PORT = “”

EMAIL_HOST_USER = “”

EMAIL_HOST_PASSWORD = “”

These are what send_mail(once we call it in our function) will look for and use to send the email we want. For us to fill the variables above, we have to go to Mailgun and go to their SMTP Credentials. And retrieve the needed information for each variable above. Note: Based on my understanding, the process is basically Django’s send_mail requires these variables to know who is sending the email and from where(which server).

Since we are placing our EMAIL_HOST_USER and our EMAIL_HOST_PASSWORD on our settings.py it will automatically be added to git and github unless we add it to our .env. So, we can create a .env file and create and from there add our EMAIL_HOST_USER and EMAIL_HOST_PASSWORD.

After placing the necessary information, we have to install django-dotenv. On the terminal we can install by pipenv install django-dotenv.

After successful installation, all we have to do is go to our Manage.py and from there, import dotenv and before we call main(), we will include dotenv.read_dotenv()

Now that our .env file is being recognized by Django (since we put it on manage.py and placing it before main() is called, Django is now aware that we have a .env file and it will read the contents.

After that, we can now retrieve the data from our .env file by placing it on the variables EMAIL_HOST_USER and EMAIL_HOST_PASSWORD. We can place this by: os.environ.get(“MAILGUN_USER”) and os.environ.get(“MAILGUN_PASSWORD). By doing this, we are retrieving the variables from our .env file and placing it inside our settings.py.

Now that we are able to do that, we can now make sure or double check if .env is in our .gitignore so the sensitive information (username/password) will not be uploaded.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creating a Line Chart in Swift and iOS

Golang Weekly Issue 39

Iframe Performance Part 2: The Good News

Code discovery: how to find code that you need to fix or extend

Designing a programing language (I)

Microblog: adding estimated reading time to my blog posts

Abstracting your way out with ReasonML

HBase Incremental Table Backup and Disaster Recovery Using AWS S3 Storage

fig1. Data backup (https://www.i-evolve.com/our-story/blog/tips-and-considerations-data-backup)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Renzo Regio

Renzo Regio

More from Medium

DANISH-BAVARIAN MATCHMAKING TOWARDS HORIZON 2020 SUCCESS!

CONNECTION WITH NATURE

On the brim of nothingness

A Full Circle Moment with Grandpa D.