【问题标题】:Sending(From) Mail configuration in django在 django 中发送(发件人)邮件配置
【发布时间】:2020-06-14 13:44:03
【问题描述】:

我在 settings.py 文件中配置了电子邮件 SMTP 配置,如下所示

  • EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
  • EMAIL_USE_TLS = 真
  • EMAIL_HOST = 'smtp.gmail.com'
  • EMAIL_HOST_USER = '来自邮件'
  • EMAIL_HOST_PASSWORD = '密码'
  • EMAIL_PORT = 587

现在它工作正常,但我的问题是部署项目后如何通过用户输入表单更改 from_mail/password 等任何参数...只需更新/更改电子邮件或密码。

【问题讨论】:

  • 在 settings.py 中,您需要从不同的文件(如 yml 文件)导入凭证,这样没有人可以看到凭证。并在.gitignore 中添加 yml 文件,这样我就不会上传到存储库中。如果你想从用户那里获取电子邮件和密码,你只需要send_mail 函数来做到这一点。 settings.py 的凭据不需要在那里。

标签: django


【解决方案1】:

你可以在django email函数的函数调用处随时更改参数:

send_mail(
 'Subject here',
 'Here is the message.',
 'from@example.com',
 ['to@example.com'],
 fail_silently=False,
)

这里解释了如何设置 auth_user/auth_password: Django Docs Sending email

【讨论】:

    【解决方案2】:

    首先,Django 文档明确指出您不应在运行时更改设置。

    https://docs.djangoproject.com/en/2.2/topics/settings/#altering-settings-at-runtime

    幸运的是,send_mail() 方法允许使用 auth_userauth_password 参数覆盖默认的 EMAIL_* 设置。

    send_mail(
     'Subject here',
     'Here is the message.',
     'from@example.com',
     ['to@example.com'],
     fail_silently=False,
     auth_user='new_user',
     auth_password='new_password',
    )
    

    文档:https://docs.djangoproject.com/en/2.2/topics/email/#send-mail

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2017-02-22
      • 1970-01-01
      • 2016-03-10
      • 2023-03-26
      • 2011-03-02
      • 2016-01-28
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多