[英]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')
.当我指定 EMAIL_HOST_USER 和 EMAIL_HOST_PASSWORD 直接电子邮件发送正确但如果尝试使用 os.environ 它会
(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')
以下错误(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.我试图在 .bashrc 和 .bash_profile 中添加变量,但没有奏效。 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 = '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')
This file is in the same directory where settings.py exist.该文件位于 settings.py 所在的同一目录中。
import os
export EMAIL_HOST_USER = 'name@gmail.com'
export EMAIL_HOST_PASSWORD = '9538913650'
below lines I have tried in terminal both in global and vertual envirnment.下面几行我在全局和虚拟环境中都在终端中尝试过。
EMAIL_HOST_USER=name@gmail.com
EMAIL_HOST_PASSWORD=123456789
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.首先,不要使用
os.environ.get('...')
- 当环境变量丢失时,它会默默地失败。 Use os.environ['...']
instead.使用
os.environ['...']
代替。
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.接下来,
.bashrc
或.bash_profile
只有在您从提供这些文件的 shell 中运行 Django 时才有效。 Remove the import os
, it is not Python.删除
import os
,它不是 Python。
Next, you still need the export
in your shell if you set the variables before running Django.接下来,如果您在运行 Django 之前设置了变量,您仍然需要在 shell 中
export
。
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(...)
.如果要在 Python 中设置环境变量,请将
os.environ
视为 dict 而不是尝试调用.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.最后,即使这适用于您的本地机器,当您部署在具有不同 IP 地址的服务器上时,它也可能会停止工作。 Every week I see questions on Stack Overflow where users are struggling to send emails from Django using gmail.
每周我都会在 Stack Overflow 上看到一些问题,其中用户正在努力使用 gmail 从 Django 发送电子邮件。 I usually suggest that they think about using a different email provider.
我通常建议他们考虑使用不同的电子邮件提供商。
Have you tried the decouple
library?您是否尝试过
decouple
库? Here is a good example: https://simpleisbetterthancomplex.com/2015/11/26/package-of-the-week-python-decouple.html这是一个很好的例子: 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 文件(并在需要时将其添加到 .gitignore):
# .env (save in the same folder as manage.py)
EMAIL_HOST_USER = 'my_email@some_url.some_extension'
又浪费了 2 个小时...你只需要重新启动你的电脑,它就会与你的原始代码一起工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.