Sending Emails in Django Application using Mailjet

beautiful paper boats on solid surface by Miguel Á. Padriñán from Pexels

https://www.pexels.com/photo/paper-boats-on-solid-surface-194094/

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? 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 you send to your users. For example 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.

  • Web API
  • SMTP relay . !!! note SMTP stands for Simple Mail Transfer Protocol.

I use smtp all the time. 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 settings.py 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 .txt file on your site or by using DNS. If you go to the list of 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 from mailjet. 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.

SMTP

Configuring Django with mailjet

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

EMAIL_HOST = 'host-name'
EMAIL_PORT = 587
EMAIL_HOST_USER = "your usernme credentials"
EMAIL_HOST_PASSWORD = "your password"
EMAIL_USE_TLS = True

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)