[英]Send email using django without a settings file
Is it possible to do something like the following using the utility django sendmail?是否可以使用实用程序 django sendmail 执行以下操作?
>>> import os
>>> from django.core.mail import send_mail
>>> os.environ['EMAIL_BACKEND'] = 'django.core.mail.backends.smtp.EmailBackend'
>>> os.environ['EMAIL_HOST'] = 'smtp.sendgrid.net'
... etc.
>>> send_mail(
... 'Subject here',
... 'Here is the message.',
... 'from@example.com',
... ['to@example.com'],
... fail_silently=False,
... )
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured.
django.core.exceptions.ImproperlyConfigured:请求设置 EMAIL_BACKEND,但未配置设置。 You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()。
If so, how could I do this?如果是这样,我该怎么做?
Yes, you can use Django without a settings.py
file.是的,您可以在没有
settings.py
文件的情况下使用 Django。 As the error message states, you'll have to call settings.configure()
to manually configure settings.如错误消息所述,您必须调用
settings.configure()
来手动配置设置。
>>> from django.conf import settings
>>> settings.configure(EMAIL_HOST='smtp.sendgrid.net', other_settings...)
Pass the settings that you want to override to configure()
function;将要覆盖的设置传递给
configure()
函数; otherwise Django will use the default values.否则 Django 将使用默认值。
See related docs for more: Using settings without setting DJANGO_SETTINGS_MODULE
.有关更多信息,请参阅相关文档: 使用设置而不设置
DJANGO_SETTINGS_MODULE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.