Django send_mail不适用于mailgun

时间:2018-11-13 17:27:19

标签: python django django-views mailgun django-email

设置:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'donotreply@kamiltrojnar.pl'
EMAIL_HOST_PASSWORD = 'passwordsecret'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'donotreply@kamiltrojnar.pl'
SITE_ID = 2

控制台中的输出:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Testing email sending 
From: donotreply@kamiltrojnar.pl
To: kamil.trojnar@gmail.com
Date: Tue, 13 Nov 2018 17:20:27 -0000
Message-ID <154212962720.11272.17395367179025062268@DESKTOP-EG7NCN4.home>

查看:

def index(request):
    send_mail("Testing email sending", 'AS  title','donotreply@kamiltrojnar.pl',['kamil.trojnar@gmail.com'], fail_silently=False)

    return render(request, 'userpanel/index.html')

我没有收到任何错误,但是没有发送电子邮件。可能是什么问题?

1 个答案:

答案 0 :(得分:5)

您正在使用控制台电子邮件后端,因此电子邮件会打印在控制台中,但不会发送:

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

要使用SMTP服务器设置,请使用SMTP backend

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

或者,您可以使用支持mailgun的应用程序,例如django-anymail。然后,您可以将mailgun API密钥添加到设置中,而不是SMTP设置中,后端将使用mailgun API发送电子邮件。