Search code examples
pythondjangodjango-viewsmailgundjango-email

Django send_mail does not work with mailgun


Settings:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'passwordsecret'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '[email protected]'
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: [email protected]
To: [email protected]
Date: Tue, 13 Nov 2018 17:20:27 -0000
Message-ID <[email protected]>

View:

def index(request):
    send_mail("Testing email sending", 'AS  title','[email protected]',['[email protected]'], fail_silently=False)

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

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


Solution

  • 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.