Sending Emails in Django Application using Mailjet

published on: | by cindy In category: Django

Don't we all hate that extra step of being told to confirm your registration by clicking a token/link in your email? I am guilty as charged. But wait, have you ever received a newsletter you never signed up for right in your email? That's annoying too. I believe sending emails is inevitable if your goal is to build and maintain a strong relationship with your clients. The aim is to attract readers, keep them updated and satisfied so that they keep coming back as well as attract new users.

Why is Email communication Important?

To answer this, I will categorize the type of emails we usually send into two main groups

  1. Marketing Emails: I like to think of it as one-to-many kind of email. These are those emails you can send to a list of subscribers enlightening them on the products/services you have on sale, a new product you are launching soon. Your guess is as good as mine by now what a great marketing tool!
  2. Transactional emails. This is a one-to-one email. These are the kind you send to your users when sending confirmation link before a new registration is active, sending welcome messages for new signups, password resets e.t.c. These types of email can greatly improve user experience.

Take confirming user registration for example. It's extra work but its important to ensure:

  • You are not bothering the wrong person.
  • Verify the existence of email addresses and will greatly reduce bounce rates
  • Reduce spammers
  • Ensure sensitive information you will send via email reach the intended recipient

Sending emails

Let start sending emails from our Django application. There are two main ways of sending emails from your application.


SMTP stands for Simple Mail Transfer Protocol.

we will use SMTP for this article. To send emails you must have an email provider or for testing purposes, Django provides a console backend which allow you to see the email which could have been sent on a console. To use the console backend, specify it in the file.

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

Which Email Provider Do I Choose From?

There are many email providers to choose from and many factors to consider too based on your requirements. A personal blog or a small company with low traffic, may prioritize cost and go for a free service with scalability. The decision is mostly influenced by cost, traffic and customer service the said provider offers. I will highlight just but a few below.

  1. Gmail: Gmail is free and primarily for personal use.Perfect for testing purposes and if you don't have a domain name. In production, you may need emails associated with your domain name.
  2. Sendgrid
  3. Mailgun: Maigun also works very well with Django and the documentation is great. There is a free account version for testing purposes but to send customized domain emails, you need to upgrade by providing credit card details to activate up to 10,000 emails for free.
  4. Mailjet: I love mailjet because it is very pocket-friendly and offers scalability as you grow, just what I need! When starting out and your site has few users, mailjet have your back.You can send up to 200 free emails every day including both transactional and marketing emails. That is 6000 free emails per month.

Why Choose Mailjet

  • The pricing is scalable, more of "pay for what you use".
  • You can use the same account for sending both transactional and marketing emails.
  • There is a free plan of up to 200 emails daily both transactional and marketing.

After successful login, go to accounts settings on the top right to start your configurations. Account Information

  1. The first step is to add sender addresses(email where we will be sending our emails from), and we can do that by :

    • Adding Domain: You Key in the actual Domain name and label it as you wish in the label field. The advantage of using domain is that all senders associated with the domain name will automatically be validated Add Domain To confirm ownership of the domain, Mailjet has two options.

      1. To Host a temporary file on your website by creating a text file with the following name:

        filename.txt // replace filename with actual text
      2. Create a DNS record After successfully verifying that you own the domain by adding a .txt file on your site or by using DNS. If you go to the list of the domain, the status should be active. List ofDomain

    • To add Addresses: It is Highly recommended that you add the sender address if you are on free hosting. Specify if you want to send transactional emails, for example, welcome emails, bulk emails such as in the case of newsletters or choose both/I don't know option.

      Add sender Adress

  2. The second step is to Configure the SPF and DKIM settings for all your domains. SPF and DKIM are authenticating systems ensuring that emails are delivered. These settings allow email services like Gmail to accept emails. Read more on spf&dkimguide To authenticate, you need to access your DNS records from your hosting provider and copy the SPF and DKIM values from mailgun to your DNS records. Datatocopy

    Once Verified,status should change from pending to active

  3. The final step is to get the SMTP settings.

    you can Use API, SMTP IS just a personal choice. Remember to keep your key and passwords away from everyone else.


Configuring Django with mailjet

Now that we are done configuring mailjet,lets use it in django to start sending emails.go to and add the following:

EMAIL_HOST = 'host-name'
EMAIL_HOST_USER = "your usernme credentials"
EMAIL_HOST_PASSWORD = "your password"

How to send Email in your Django Application

Django uses a module called django.core.mail The basic way to send email is using django.core.mail.send_mail(). It takes the parameters:

  • subject: The subject of the email and its a string.
  • message: The body of the email also takes a string.
  • from_email: Specify the sender.
  • recipient_list: List of recipients
  • fail_silently − Bool, if false send_mail will raise an exception in case of error.
from django.core.mail import send_mail
send_mail(subject,message,sender email,[receipient email], fail_silently=False)
cindy achieng


I am a mobile-web developer highly skilled in django and Co-founder of Oark Limited a tech company based in Nairobi Kenya. I love to share my coding journey with the world with the aim to teach ,learn and inspire. More about Cindy