DEV Community

Cover image for Adding ESP to supercharge your Django Email 🚀
Paul
Paul

Posted on • Originally published at foxcraft.hashnode.dev

Adding ESP to supercharge your Django Email 🚀

Whether you are trying to build your own SAAS or building a project for client a its very important to have email integration for sending invoice, newsletter's, updates and more.

Using the default Django's SMTP email is not scalable, with emails hitting the per day limits or going to spam folders.

Email Service Providers (ESPs) offer scalable, highly deliverable, trackable, and compliant email solutions. Many ESPs provide API access, enabling you to send, schedule, and customize emails efficiently. Setting up an ESP for your transactional emails is quick and straightforward, often taking less than 15 minutes, making it a highly recommended addition to your email strategy.

There are many email service providers such as Sendgrid, Brevo, MailGun, Amazon SES etc.

In this tutorial we'll see how to add Brevo ESP (formally sendinblue) to Django.

Before we get started make sure to signup to Brevo and fill in the details to keep your email complainant.

Django Setup for ESP

We'll be using Anymail package as it offers API compatible with Django's existing email and can be configured for any ESP.

Start by installing anymail

pip install django-anymail[brevo]
Enter fullscreen mode Exit fullscreen mode

Now go to you settings.py and add anymail to INSTALLED_APPS

INSTALLED_APPS = [
    .
    .
    'anymail',
]
Enter fullscreen mode Exit fullscreen mode

Now add brevo as your email backend

EMAIL_BACKEND = "anymail.backends.brevo.EmailBackend"

BREVO_API_URL = "https://api.brevo.com/v3/" # optional

ANYMAIL = {
                "BREVO_API_KEY": 'BREVO_API_KEY', # use brevo api key 
                "IGNORE_RECIPIENT_STATUS": True,
            }
DEFAULT_FROM_EMAIL = 'ben@example.com'  # default from email
Enter fullscreen mode Exit fullscreen mode

Anymail works with default Django email, so now import send_mail

from django.core.mail import  send_mail

subject = "Test transactional email"
body = f"Hi\nthis is a test email"
html_message = """
 Hi,<br> This is a brevo test mail.
"""

send_mail(subject, message=body, from_email=None, 
          recipient_list=['james@example.com'], 
            html_message=html_message)
Enter fullscreen mode Exit fullscreen mode

The html_message is necessary to send via Brevo, so ensure you add it. If the from_email is set to None It uses the default mail you set up in the settings.py file.

If you need to add cutsom parameters for brevo to fill in, just use {{params.name}} read more about Brevo templating

That's all, all you need is you Brevo API key and you'll be able to send messages via Brevo

Brevo API Key

Go to the top right corner dropdown -> click on SMTP & API

Brevo API Key

Now from the API's tab, click on generate new API key

generate brevo key

Copy the key and Paste it in the settings.py file and you are good to go.

When you send your first test email, you may get a email from Brevo asking you to add it to authorized IP, once you set it, it'll start sending the emails.

You can also disable this by going to profile dropdown -> Security ->Authorized IPs

Brevo authorized IP

Advanced usage

You can also create templates in Brevo and send customized bulk emails with just one API call.

Start by creating a template. Follow the steps in Brevo to create the template.

Now to send the via template lets make some changes to the code

from django.core.mail import EmailMultiAlternatives

recipient_list = ['ben@example.com', 'james@example.com']
from_email = None # uses default email address
email = EmailMultiAlternatives(from_email=from_email, to=recipient_list)

email.template_id = 8 # your brevo template id

email.merge_data = {
    'ben@example.com': {'name': "Ben", 'order_no': "12345"}, # customize per customer
    'james@example.com': {'name': "James", 'order_no': "54321"},
}
email.merge_global_data = {
    'ship_date': "May 15", # this is common global data for both ben and james
} 

email.send()
Enter fullscreen mode Exit fullscreen mode

That's it now you can also send emails using Brevo template's.

If you have any questions, drop a comment.

Top comments (0)