DEV Community

Destiny Franks
Destiny Franks

Posted on

Send Email using Django & Mailgun - Easy and Free

In this short posts i would be showing how to quickly send email to email addresses using django, malgun and anymail packages.

Anymail lets you send and receive email in Django using your choice of transactional email service providers (ESPs). It extends the standard django.core.mail with many common ESP-added features, providing a consistent API that avoids locking your code to one specific ESP (and making it easier to change ESPs later if needed).

Anymail currently supports these ESPs:

Amazon SES
MailerSend
Mailgun
Mailjet
Mandrill (MailChimp transactional)
Postal (self-hosted ESP)
Postmark
SendGrid
SendinBlue
SparkPost

Mailgun is an email automation service built for developers. It offers a complete cloud-based email service for sending, receiving and tracking emails sent through your websites and applications. Mailgun's intelligent inbound routing and storage enables you to know exactly where your emails are ending up.

And finally Django is a free and open-source, Python-based web framework that follows the model–template–views architectural pattern and trust me it is really awesome i'd say.

Let's quit talking and get to work

  1. Firsly, you need to install the anymail package
pip install "django-anymail[mailgun]"
Enter fullscreen mode Exit fullscreen mode
  1. Add anymail to django installed app
INSTALLED_APPS = [
    # ...
    "anymail",
    # ...
]
Enter fullscreen mode Exit fullscreen mode
  1. In your settings.py add your Mailgun api keys and sanbox domains for testing purposes
ANYMAIL = {
    "MAILGUN_API_KEY": "api_key_305834958304534",
    "MAILGUN_SENDER_DOMAIN": 'sandbox467c****.mailgun.org',  
}
EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend" 
DEFAULT_FROM_EMAIL = "your-sender-email@example.com" 
SERVER_EMAIL = "your-sender-email@example.com"  
Enter fullscreen mode Exit fullscreen mode

NOTE: you would need to create an account with mailgun, after successful account creation, follow these steps
i. Login to dashbaord
ii. Sendig
iii. Domain (suboption)
iv. CLick on the sandbox domain url
v. CLick on the api keys
vi. Copy your secret api key and paste above.

For testing purposes, you need to add the recipient email address in the box at the right side of the dashboard
Here is an image

Image description

If you have a paid account, you do not need to do this.

After all these are done, create a simple function based view to send the email in views.py

from django.core.mail import EmailMultiAlternatives, send_mail
def send_mail_func(request):
    ...
    send_mail("New Order!", "Hey buddy, you have a new order",
          "sender-email@gmail.com", ["reciever-email@gmail.com"])
    return HttpResponse("Email Sent")
Enter fullscreen mode Exit fullscreen mode

COnfigure the url, and visit this page on your browser.
It should be working now, it did for me.

Here is the anymail api doc

Top comments (1)

Collapse
 
desphixs profile image
Destiny Franks

If you have any issue, let me know!