Sending Email in Django from Gmail

By Justin

Sending Email in Django from Gmail
Long term, I do not recommend using Gmail to email you users. Instead you should upgrade to a managed transactional email service.
That being said, using gmail can be an effective way to test all kinds of email related activities. This blog post was written for the SaaS Foundations course but you should be able to use it in any Django project.
A few things you should really think about when going down this route:
  • Never spam people
  • Never use your personal gmail account (e.g. it's tied to any sensitive accounts)
  • Never put your password in code
  • Never forget to activate 2-Factor Authentication for your Gmail Account (all accounts really)
  • Never spam

Step 1: Create a Gmail account

Obviously create a new account. Never use your personal gmail account. Creating a new gmail account is free and easy.

Step 2: Enable 2-Factor Authentication

Enable as many 2-Factor Authentication options you can.

Step 3: Create an App Password

  1. Search for "App Password"
  2. Create an "App Password". These are easy to delete and recreate, do this as needed.
  3. Copy the App Password for your Django project's environment variables. Such as. a dotenv file .env or in your production systems environment variables (over time you should upgrade to a domain you own with a production-ready transactional email service)
Thanks to CodeWithMuh for sharing this tip!

Step 4: Update Django Configuration

In your main settings module (settings.py), add the following configuration:
python
from decouple import config

# default backend
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = config("EMAIL_HOST", cast=str, default=None)
EMAIL_PORT = config("EMAIL_PORT", cast=str, default='587') # Recommended
EMAIL_HOST_USER = config("EMAIL_HOST_USER", cast=str, default=None)
EMAIL_HOST_PASSWORD = config("EMAIL_HOST_PASSWORD", cast=str, default=None)
EMAIL_USE_TLS = config("EMAIL_USE_TLS", cast=bool, default=True)  # Use EMAIL_PORT 587 for TLS
EMAIL_USE_SSL = config("EMAIL_USE_SSL", cast=bool, default=False)  # EUse MAIL_PORT 465 for SSL
Notice I am using python-decouple to load environment variables. Use whatever tool you want but never set sensitive data in the code, even during development.
My .env file looks like this:
bash
EMAIL_HOST="smtp.gmail.com"
EMAIL_PORT=587
EMAIL_HOST_USER="[email protected]"
EMAIL_HOST_PASSWORD="my google account app password not my user password"
EMAIL_USE_TLS=True

Step 5: Add Django Managers and Admins

Again in settings (settings.py), add the following:
python
ADMIN_USER_NAME=config("ADMIN_USER_NAME", default="Admin user")
ADMIN_USER_EMAIL=config("ADMIN_USER_EMAIL", default=None)

MANAGERS=[]
ADMINS=[]
if all([ADMIN_USER_NAME, ADMIN_USER_EMAIL]):
    ADMINS +=[
        (f'{ADMIN_USER_NAME}', f'{ADMIN_USER_EMAIL}')
    ]
    MANAGERS=ADMINS
Add to .env:
bash
ADMIN_USER_EMAIL=""

Step 6: Test Email

bash
python manage.py sendtestemail --admins
or
bash
python manage.py shell
python
from django.conf import settings
from django.core.mail import send_mail

ADMIN_USER_EMAIL= settings.ADMIN_USER_EMAIL
EMAIL_HOST_USER = settings.EMAIL_HOST_USER

send_mail(
    "Test Message In Python",
    "Might be working now!",
    EMAIL_HOST_USER
    [ADMIN_USER_EMAIL],
    fail_silently=False,
)
Do you need a guide on a production version of this? Let us know in the comments which service you would want us to show.
Discover Posts
Sending Email in Django from Gmail