52. Configuring email sending in Django - password reset
Now we will begin developing the functionality to send emails via Django to users with their purchase details. This will address two key use cases:
Sending purchase details to clients after a successful transaction.
Facilitating password resets for clients.
In the settings.py
file, the EMAIL_BACKEND
variable currently outputs email content to the terminal console as determined previously. We will modify this configuration to use Djangoโs SMTP feature for sending emails.
Change the variable from this:
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
To this:
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
Note: We will use Gmail as our reference for three main reasons:
It offers a secure configuration.
It is widely used by individuals.
Many companies use Gmail to manage their domains.
First, click on "Manage your Google Account" from your Gmail profile picture (you will need to use your own Gmail account for this process):

Next, click on safety:

We will use a Gmail feature called App Passwords. However, you first need to enable two-step verification for your account (if you haven't already).
After that, go to the search box at the top and type "App Passwords".

Click on the option above, and you will be redirected to the App Passwords page.

Give the app a name and click "Create." A password will appear on your screen; make sure to write it down or save it, as Gmail will not display it again. This password grants full access to your account, so it will not be displayed here.
At the end of the settings.py
file, we will define a set of variables to configure the email system for our website.
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587 #? port gmail uses
EMAIL_USE_TLS = True #? encryption
EMAIL_HOST_USER = "youremailgoeshere"
EMAIL_HOST_PASSWORD = "passwordgoeshere"
Note:
The
EMAIL_HOST_PASSWORD
should be the password generated earlier, entered without spaces, and theEMAIL_HOST_USER
should be your Gmail address.The
EMAIL_PORT
andEMAIL_HOST
are specific to Gmail. If you use a different email provider (other than Gmail-managed custom domains), you will need to look up their respective ports and hosts for Django online.
Now, log out of your e-commerce website.
On the login page, click "Forgot Password," enter an email address that is registered on your site, and click "Reset My Password" (ensure you have access to this inbox).
After doing so, you will receive an email with instructions for resetting your password.


Last updated