Django send_mail 不适用于 mailgun
Django send_mail does not work with mailgun
设置:
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')
我没有收到任何错误,但电子邮件未发送。可能是什么问题?
您正在使用控制台电子邮件后端,因此电子邮件会在控制台中打印但不会发送:
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 发送电子邮件。
设置:
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')
我没有收到任何错误,但电子邮件未发送。可能是什么问题?
您正在使用控制台电子邮件后端,因此电子邮件会在控制台中打印但不会发送:
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 发送电子邮件。