Reputation: 89
When I specify EMAIL_HOST_USER and EMAIL_HOST_PASSWORD directly email send properly but if try to get using os.environ it is throwing following error (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError l26sm20714449pgn.46 - gsmtp', 'webmaster@localhost')
. I have tried to add variables in .bashrc and .bash_profile but it didn't worked. What I have tried shown below. can anyone help me this please.
variables shown below.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
# EMAIL_HOST_USER = '[email protected]'
# EMAIL_HOST_PASSWORD = '123456789'
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
print('variable :',os.environ.get('EMAIL_HOST_USER'))
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
This file is in the same directory where settings.py exist.
import os
export EMAIL_HOST_USER = '[email protected]'
export EMAIL_HOST_PASSWORD = '9538913650'
below lines I have tried in terminal both in global and vertual envirnment.
[email protected]
EMAIL_HOST_PASSWORD=123456789
code shown below.
import os
os.environ.set('EMAIL_HOST_USER') = '[email protected]'
os.environ.set('EMAIL_HOST_PASSWORD') = 12345678
Upvotes: 0
Views: 1332
Reputation: 9
Just wasted another 2 hours... you only need to restart your computer and it will work with your original code
Upvotes: 0
Reputation: 308939
Firstly, don't use os.environ.get('...')
- it silently fails when the environment variable is missing. Use os.environ['...']
instead.
EMAIL_HOST_USER = os.environ['EMAIL_HOST_USER']
print('variable :',os.environ['EMAIL_HOST_USER'])
EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD']
Next, the .bashrc
or .bash_profile
will only work if you are running Django from a shell that has sourced those files. Remove the import os
, it is not Python.
Next, you still need the export
in your shell if you set the variables before running Django.
export [email protected]
export EMAIL_HOST_PASSWORD=123456789
If you want to set the environment variables in Python, then treat os.environ
as a dict instead of trying to call .set(...)
.
import os
os.environ['EMAIL_HOST_USER'] = '[email protected]'
os.environ['EMAIL_HOST_PASSWORD'] = 12345678
Finally, even if this works on your local box, it might stop working when you deploy on a server with a different IP address. Every week I see questions on Stack Overflow where users are struggling to send emails from Django using gmail. I usually suggest that they think about using a different email provider.
Upvotes: 1
Reputation: 3527
Have you tried the decouple
library? Here is a good example: https://simpleisbetterthancomplex.com/2015/11/26/package-of-the-week-python-decouple.html
Usage:
# settings.py
from decouple import config
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
Then create a .env file (and add it to .gitignore if needed):
# .env (save in the same folder as manage.py)
EMAIL_HOST_USER = 'my_email@some_url.some_extension'
Upvotes: 0