简体   繁体   English

无法为 django settings.py 导入环境变量以在 linux ubuntu 系统中发送电子邮件

[英]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.谁能帮我这个请。

settings.py设置.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 .bashrc

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'

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使用 python 命令

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.

相关问题 如何在django项目中导入custormed settings.py变量? - how to import the custormed settings.py variable in django project? 在Django的settings.py中导入模型 - Import model in Django's settings.py django settings.py中的“os.environ”无法使用apache和wsgi获取系统环境变量 - “os.environ” in django settings.py cannot get system environment variables with apache and wsgi Python、Django - 访问 settings.py 中的环境变量 - Python, Django - Accessing Environment Variables in settings.py Django settings.py错误:不支持按文件名导入 - Django settings.py Error: Import by filename is not supported Django settings.py 未更新 - Django settings.py not updating Django部署:ImportError:无法导入设置“ settings.py”(在sys.path上吗?):没有名为settings.py的模块 - Django Deployment: ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named settings.py settings.py中的Django 1.4“LOGGING”变量似乎被忽略了 - Django 1.4 “LOGGING” variable in settings.py seems to be ignored django procfile找不到settings.py - django procfile can't find settings.py 无法在 django 中保存对我的 settings.py 文件的更改 - Can't save changes to my settings.py file in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM