即使conf正确,Django也无法发送电子邮件

问题描述 投票:0回答:1

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
python python-3.x django email django-email
1个回答
0
投票

您正在使用

console.EmailBackend
后端,该后端在开发模式下用于测试目的。

要通过 SMTP 服务器发送电子邮件,您需要将电子邮件后端设置为

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

文档

中阅读更多内容
© www.soinside.com 2019 - 2024. All rights reserved.