Django SMTPAuthenticationError when using the same variable name for EMAIL_HOST_PASSWORD in .env and settings.py

I am working on a Django project where I use Gmail for email services. I have the following configuration in my .env file:

EMAIL_HOST_USER=gmail@gmail.com

EMAIL_HOST_PASSWORD=password

And in my settings.py, I load these variables as follows:

EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')

EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')

When I try to send an email through my application, I encounter an SMTPAuthenticationError.

However, when I change the name of the variable in the .env file, it works fine, but the password is no longer masked. How can I fix this while keeping the password masked?

Use this:

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = os.environ.get("EMAIL_HOST")
EMAIL_PORT = os.environ.get("EMAIL_PORT")
EMAIL_USE_TLS = os.environ.get("EMAIL_USE_TLS")
EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD")
DEFAULT_FROM_EMAIL = os.environ.get("EMAIL_HOST_USER")
Вернуться на верх