简体   繁体   English

使用没有设置文件的 django 发送电子邮件

[英]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.

相关问题 不使用发件人地址的Django电子邮件设置 - Django Email settings without using From Address 如何在不使用 django 在 python 中使用 celery 发送电子邮件 - How to send email with celery without using django in python Django rest framework: sendgird email with attachment without model only filefield to open file and send button to send email - Django rest framework: sendgird email with attachment without model only filefield to open file and send button to send email 使用xlwt创建文件并在Django应用程序中发送电子邮件 - Create a File using xlwt and send email in Django application 如何在不使用smtplib的情况下使用Python发送带有嵌入式图像文件的电子邮件? - How to send email with embedded image file in Python without using smtplib? 将生成的CSV文件附加到电子邮件并使用Django发送 - Attach generated CSV file to email and send with Django Django-如何在不使用Django表单的情况下获取文件输入并将文件附加到电子邮件 - Django - how to get file input without using django form and attach the file to email 如何使用 Celery(无第三方包)发送 Django 的密码重置邮件? - How to send Django's password reset email using Celery (without third-party package)? 如何使用Django将输入的详细信息发送到电子邮件 - How to send entered details to email using django 使用 Django EmailBackend 发送 email 的问题 - Problems to send email using Django EmailBackend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM