Django send_mail does not work with mailgun

Viewed 3502

Settings:

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

Output in console:

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>

View:

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')

I get no errors but the email is not sent. What could be the issue?

1 Answers

You are using the console email backend, so emails are printed in the console but are not sent:

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

To use the SMTP server settings, use the SMTP backend:

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

Alternatively, you could use a library that supports mailgun, for example django-anymail. Then you would add your mailgun API key to your settings instead of SMTP settings, and the backend would use the mailgun API to send the emails.

Related