Introduction
On your software development journey, you'll likely want to integrate email to test event-driven email sending. Pre-deployment testing of this feature requires an email client to verify functionalities.
While you might initially think you need endless bandwidth for mass testing, that's not necessarily the case.
Gmail, a product of Google and one of the most popular email services, is a great option for email testing. I've personally found it highly effective for solving this issue over the past five years of development.
In this article, I'll break down the three steps of Gmail integration with Django.
- Log in to your Gmail account. In the top right corner, click your profile picture and then select the "Manage account" option.
2.
Go to the search bar and search "App Passwords" and click on the matching option:
The information you need to provide will depend on your account's security settings. Once you meet all the security checks, you'll be redirected to a page where you can name your app and create it.
Afterward, the following will be prompted showing the password.
copy the password to a secure location.KEEP IT SECRET
3.
Django Email Integration
In production environments, it's crucial to secure your passwords. A common approach is to use a .env file to store your email credentials. Here's how to set it up:
- Create the following variables in your .env file:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your-gmail-email-address'
EMAIL_HOST_PASSWORD = 'password-generated-above-from-app-passwords'
- Open the project settings file and add the following settings
from dotenv import load_dotenv
load_dotenv()
EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')
EMAIL_USE_SSL = os.getenv('EMAIL_USE_SSL')
EMAIL_PORT = os.getenv('EMAIL_PORT')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
Congratulations! you have successfully set up Django to send emails.
Thank you.
Top comments (0)