Django Send Email Using Mailgun Api

Django Send Email Using Mailgun Api

We can send email in django using mailgun api. Mailgun is one of the trusted email service providers. When we send email using the mailgun it can directly deiver it to the recipient. As it is a trusted email service provider our email cannot be spammed. So, our information can be reached to the end user. By using mailgun we can send upto 10,000 emails for free per a month. We can get a free account from mailgun. So, what are you waiting for folks ? Let's check it out.

How to integrate it mailgun in Django?

One of the coolest thing about Django is that it has a global community support. So, what we can expect ? We can find pip packages for anything. To integrate mailgun with django we use pip package "django-mailgun". Let's see how to integrate it with django app.

  1. install package "django-mailgun"
    pip install django-mailgun
    
  2. Configure mailgun email backend in settings.py file
    EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
    MAILGUN_ACCESS_KEY = 'ACCESS-KEY'
    MAILGUN_SERVER_NAME = 'YOUR-DOMAIN-NAME'
    EMAIL_PORT = 587
    
  3. Let's test our mailgun configuration by sending an email & open django shell by typing "python manage.py shell"

    from django.core.mail import EmailMultiAlternatives
    
    subject = "Hello, its me"
    text_content = "I was wondering if after all these years"
    sender = "[email protected]"
    recipient = "[email protected]"
    msg = EmailMultiAlternatives(subject, text_content, sender, [recipient])
    msg.send()
    

Replace above sender and recipient emails with your emails to make it work. Let me know how it worked for you in the below comments.