姜戈电子邮件
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当任何邮件发送函数传递
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_EMAIL 或 settings.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 indjango/core/mail.py
.When there is an unhandled exception Django calls the
mail_admins()
function indjango/core/mail.py
which always uses settings.SERVER_EMAIL and is only sent to addresses listed in settings.ADMINS. This can also be verified indjango/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 thefrom_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].