Django 无法发送电子邮件。
但是 https://www.smtper.net/ 能够发送具有相同确切设置、用户、密码的测试电子邮件。
我需要在 django 中做更多的事情来发送电子邮件吗?
设置.py
## #environment variables from .env.
from dotenv import load_dotenv
load_dotenv()
NOREPLYEMAIL = os.getenv('NOREPLYEMAIL')
NOREPLYEMAILPASS = os.getenv('NOREPLYEMAILPASS')
### Email config
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtppro.zoho.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = NOREPLYEMAIL
EMAIL_HOST_PASSWORD = NOREPLYEMAILPASS
DEFAULT_FROM_EMAIL = NOREPLYEMAIL
视图.py
# using https://docs.djangoproject.com/en/5.0/topics/email/
@csrf_protect
def test(request):
testemail = EmailMessage(
"Hello", #subject
"Body goes here", #message body
NOREPLYEMAIL, #from
["[email protected]",], #to
reply_to=[NOREPLYEMAIL],
headers={"Message-ID": "test"},
)
testemail.send()
控制台
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Hello
From: [email protected]
To: [email protected]
Reply-To: [email protected]
Date: Tue, 02 Jul 2024 11:38:16 -0000
Message-ID: test
Body goes here
-------------------------------------------------------------------------------
[02/Jul/2024 17:08:16] "GET /test/ HTTP/1.1" 200 13102
console.EmailBackend
后端,该后端在开发模式下用于测试目的。
要通过 SMTP 服务器发送电子邮件,您需要将电子邮件后端设置为
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
在文档
中阅读更多内容