django中的send_mail未发送命令行sendmail是

问题描述

我有一台linux笔记本电脑。我正在尝试在django应用程序中测试一些电子邮件功能,但似乎无法使send_mail或EmailMessage正常工作。

这是我的sendmail:

def get(self,request,*args,**kwargs):
        send_mail(
            'Subject here','Here is the message.','[email protected]',['[email protected]'],fail_silently=False,)
        return HttpResponse('Message sent')

当我转到该页面时,它将在终端上打印成功的电子邮件消息:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-transfer-encoding: 7bit
Subject: Subject here
From: [email protected]
To: [email protected]
Date: Mon,26 Oct 2020 19:13:36 -0000
Message-ID: <[email protected]>

Here is the message.

我从没收到过我的Gmail邮件。我已更改为“从”和“至”以掩盖实际的电子邮件地址。我可能做错了什么?如果我使用命令行这样发送邮件,它将起作用。

发送邮件[email protected] /email.txt

解决方法

可能您需要在settings.py

中添加一些行
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.pop-os.domain'
EMAIL_HOST_USER = '[email protected]'
EMAIL_PORT = 465
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_SSL = True