gpt4 book ai didi

django - 为什么 django send_mail 无法无误地发送电子邮件?

转载 作者:行者123 更新时间:2023-12-04 00:59:54 26 4
gpt4 key购买 nike

我想在 Django 中使用 send_mail,但是它不发送任何电子邮件。这是我的设置:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'MY-GMAIL-USERNAME@gmail.com'
EMAIL_HOST_PASSWORD = 'MY-GMAIL-PASSWORD'
EMAIL_PORT = 465
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = DEFAULT_FROM_EMAIL

然后,我运行 python manage.py shell:

from django.conf import settings
from django.core.mail import send_mail

subject = 'Test Subject'
message = 'Test Message'
email_from = settings.EMAIL_HOST_USER
recipient_list = ['MY-YAHOO-USERNAME@yahoo.com']
send_mail(subject, message, email_from, recipient_list, fail_silently=False)

它不发送电子邮件,而是打印:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Test Subject
From: MY-GMAIL-USERNAME@gmail.com
To: MY-YAHOO-USERNAME@yahoo.com
Date: Mon, 09 Dec 2019 21:02:16 -0000
Message-ID: <157592533640.18842.5494330274157836181@thinkpad-e560>

Test Message
-------------------------------------------------------------------------------
1

这似乎没有错误。为什么不发送电子邮件?怎么了?为什么它不记录任何错误?

正在关注 this page ,我已经尝试了一种纯 python 方式并且它工作正常:

import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 465
sender_email = "MY-GMAIL-USERNAME@gmail.com"
password = 'MY-GMAIL-PASSWORD'
receiver_email = 'MY-YAHOO-USERNAME@yahoo.com'
context = ssl.create_default_context()
server = smtplib.SMTP_SSL("smtp.gmail.com", port, context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, 'Test Message')

正如我提到的,这种纯 Python 方式工作正常!我很困惑。

我做错了什么?

最佳答案

感谢@IainShelvington 和@ivissani,我已经弄明白了。我在 django 之上使用 wagtail cms 并使用“wagtail start mysite”创建项目。有关详细信息,请参阅 this .

在wagtail标准项目结构中,有base.py、dev.py和production.py,而不是settings.py。

电子邮件设置在 base.py 中,但在 dev.py 中,EMAIL_BACKEND 被覆盖了。这是使用 wagtail 创建 django 项目时的 dev.py:

from .base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'MY-SITE-KEY'

# SECURITY WARNING: define the correct hosts in production!
ALLOWED_HOSTS = ['*']

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'


try:
from .local import *
except ImportError:
pass

所以我通过从 dev.py 中删除 EMAIL_BACKEND 解决了这个问题

关于django - 为什么 django send_mail 无法无误地发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com