The following documents how to setup a Django application so send emails using the send_mail core function with a non-business gmail account.
Setup your google account
Google does not allow standard basic auth Gmail requests for a variety of good reasons. This means that you cannot just:
EMAIL_HOST_USER = [email protected]
EMAIL_HOST_PASSWORD = yourgmailpassword
But instead, you are required to create an application password. App passwords are free and unlimited and, ideally, you should be creating one for each application that will be sending mails.
In order to qualify for creating an app password your Google account must:
- have 2-Step Verification setup
- not use Google security keys
- not be a business or organisation account
- not have advanced protection setup
Once you are sure the above is set for your account, head to https://myaccount.google.com/apppasswords and create a new password.
Django settings
Add the following to your .env file:
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_HOST_USER=yourmail@gmail.com
EMAIL_HOST_PASSWORD=yourapppassword
EMAIL_USE_TLS=True
EMAIL_USE_SSL=False
And now wire everything up in your projects settings.py file:
from decouple import config
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_USE_TLS = config('EMAIL_USE_TLS', cast=bool)
EMAIL_USE_SSL = config('EMAIL_USE_SSL', cast=bool)
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_HOST_USER = config('EMAIL_HOST_USER', default='notavailable', cast=str)
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD', default='notavailable', cast=str)
EMAIL_PORT = config('EMAIL_PORT', cast=int)
- Note that I’m using decouple to access the aboves environment variables.
That should be enough to enable Django’s send_mail to work properly across your application.