简体   繁体   中英

Couldn't able to import environment variable for django settings.py for sending email in linux ubuntu system

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.

settings.py

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 = 'name@gmail.com'
# 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')

.bashrc

This file is in the same directory where settings.py exist.

import os
export EMAIL_HOST_USER = 'name@gmail.com'
export EMAIL_HOST_PASSWORD = '9538913650'

main

below lines I have tried in terminal both in global and vertual envirnment.

EMAIL_HOST_USER=name@gmail.com
EMAIL_HOST_PASSWORD=123456789

using python comand

code shown below.

import os
os.environ.set('EMAIL_HOST_USER') = 'name@gmail.com'
os.environ.set('EMAIL_HOST_PASSWORD') = 12345678

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_HOST_USER=name@gmail.com
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'] = 'name@gmail.com'
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.

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'

又浪费了 2 个小时...你只需要重新启动你的电脑,它就会与你的原始代码一起工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM