Sending Emails with Django using SendGrid in 3 easy steps

Sendgrid plus django plus mail
Image: Sendgrid plus django plus mail (License: CC-BY-SA Marcelo Canina)
Last modified:


Step by step guide to provide your Django app with Email capabilities.

Typically used for account activation and notifications.

This guide uses one of the most popular solutions, SendGrid, which provides a considerably free amount of emails for free each month (100 per month at the time of writing this article).

Using Sendgrid with Django

Email service can be added to Django following two strategies:

  1. Using SendGrid’s Web API or
  2. Using SMTP as the transport mechanism

In this tutorial we are going to use the first method, with a Web API. 1

To use the Web API we will rely on the the django-sendgrid-v5 package,

This package implements an email backend for Django that relies on *SendGrid*'s REST API for message delivery.

Django-sendgrid-v5 project repo


Log in to Sendgrid at

1. Get API keys

And Create an API key for the app at

We will create an API key just for sending emails, so in Name enter my_app_send_mails.

Then choose Restricted Access, enable Mail Send and hit the Create button.

A new API Key will be generated, copy and set the SENDGRID_API_KEY environment variable.

Here you can use dot-env or django-dotenv approaches, so /.env would look like:


2. Use package

Using pip we install django-sendgrid-v5: pip install django-sendgrid-v5

And then tell Django to use it.

3. Configure Keys in Django

in app/

import os

EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"


There are also optional settings to deliver emails in debug mode or to send them to standard output:

# Toggle sandbox mode (when running in DEBUG mode)

# echo to stdout or any other file-like object that is passed to the backend via the stream kwarg.

Testing it works

To test that everything works fine we will send an email.

Out of sandbox

First turn off SendGrid’s sandbox mode SENDGRID_SANDBOX_MODE_IN_DEBUG=True so we can send and receive an email from console.

Send an email

Then test sending the email with django.core.mail.send_mail like:

from django.core.mail import send_mail

    'Subject here',
    'Here is the message.',

Putting it all together:

$ python3 shell
>>>  from django.core.mail import send_mail
>>>  send_mail('testing', 'my message', '', [''], fail_silently=False)
.virtualenvs/xxx/lib/python3.6/site-packages/sendgrid_backend/ UserWarning: Sendgrid email backend is in sandbox mode!  Emails will not be delivered.
  warnings.warn("Sendgrid email backend is in sandbox mode!  Emails will not be delivered.")
>>>  from django.conf import settings
>>>  send_mail('testing', 'my message', '', [''], fail_silently=False)

And you should get an email at

Sender Authentication

We can also make some improvements, like Domain Authentication at to improve deliverability by proving to inbox providers that you own the domain you’re sending from.

Send emails from own domain

Also, instead of sending emails from accounts like …, it would be better to have them with our domain … In SendGrid this is called Link Branding and can be configured at:


  1. If you want to use SMTP sending email over SMTP with Django check Sendgrid smtp tutorial with Django email guide↩︎

Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme