姜戈电子邮件

发布于 2024-07-14 02:07:35 字数 750 浏览 7 评论 0原文

我正在使用 Gmail SMTP 服务器从我的网站用户发送电子邮件。

这些是我的 settings.py 中的默认设置。

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

如果我希望用户发送电子邮件,我将覆盖这些设置并使用 Django 的电子邮件发送方法发送电子邮件。 当系统出现异常时,我收到一封来自 [email protected]。 有时我会收到一些登录用户发来的电子邮件。 这也可能意味着当用户收到从我的网站发送的电子邮件时,它的发送地址与实际用户不同。

应该怎样做才能避免这种情况呢?

I am using the Gmail SMTP server to send out emails from users of my website.

These are the default settings in my settings.py

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

If I want a user to send an email, I am overriding these settings and sending the email using Django's email sending methods. When an exception occurs in the system, I receive an email from the [email protected]. Sometimes I receive an email from some logged in user. Which could also possibly mean that when a user receives an email sent from my website it has a sent address different from the actual user.

What should be done to avoid this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-07-21 02:07:36

当任何邮件发送函数传递 None 或空字符串作为发件人地址时,Django 仅使用 settings.DEFAULT_FROM_EMAIL。 这可以在 django/core/mail.py 中进行验证。

当出现未处理的异常时,Django 会调用 django/core/mail.py 中的 mail_admins() 函数,该函数始终使用 settings.SERVER_EMAIL 并且发送到settings.ADMINS中列出的地址。 这也可以在 django/core/mail.py 中进行验证。

Django 本身发送电子邮件的唯一其他位置是,如果 settings.SEND_BROKEN_LINK_EMAILS 为 True,则 CommonMiddleware 会将邮件发送到 settings.MANAGERS 中列出的所有地址以及电子邮件发件人是settings.SERVER_EMAIL

因此,普通用户只有在您调用 send_mail() 时才会从您的网站收到电子邮件。 因此,请始终传递真实地址作为 from_mail 参数,这样您就可以避免用户从 settings.SERVER_EMAILsettings.DEFAULT_FROM_EMAIL 接收电子邮件。

旁注:django-registration 至少是 Django 可插拔的一个示例,它将从 settings.DEFAULT_FROM_EMAIL 发送邮件,因此在这种情况下,您需要确保它是一个正确的电子邮件地址,例如[电子邮件受保护][电子邮件受保护]

Django only uses settings.DEFAULT_FROM_EMAIL when any of the mail sending functions pass None or empty string as the sender address. This can be verified in django/core/mail.py.

When there is an unhandled exception Django calls the mail_admins() function in django/core/mail.py which always uses settings.SERVER_EMAIL and is only sent to addresses listed in settings.ADMINS. This can also be verified in django/core/mail.py.

The only other place Django itself sends e-mails is if settings.SEND_BROKEN_LINK_EMAILS is True, then CommonMiddleware will send mail to all addresses listed in settings.MANAGERS and the e-mail sender is settings.SERVER_EMAIL.

Therefore, the only time a regular user will receive e-mail from your site is when you call send_mail(). So, always pass a real address as the from_mail argument and you will avoid users receiving email from settings.SERVER_EMAIL or settings.DEFAULT_FROM_EMAIL.

Side note: django-registration is at least one example of a Django pluggable that will send mail from settings.DEFAULT_FROM_EMAIL so in cases like this you need to make sure it is a proper e-mail address such as [email protected] or [email protected].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文