为什么 django send_mail 无法正确发送电子邮件?
Why does django send_mail fails to send email with no errors?
我想在 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 方式很管用!我很困惑。
我做错了什么?
用这个开始你的 python 文件:
import os
import sys
import django
django.setup()
# rest of your code
from django.conf import settings
导入您的设置,但不会将其设置为 os 环境。
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
- 这也应该有效
感谢@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 中使用 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 方式很管用!我很困惑。
我做错了什么?
用这个开始你的 python 文件:
import os
import sys
import django
django.setup()
# rest of your code
from django.conf import settings
导入您的设置,但不会将其设置为 os 环境。
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
- 这也应该有效
感谢@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 来解决问题