How to use Azure Communication Services to send email in a Django application

I wanted to share my recent experience with setting up email sending in my Django application, which I’m deploying on Azure Web Apps. Since I’m using django-allauth for authentication, I needed to ensure that the email confirmation and password reset workflows were properly configured. After some research, I found a solution that worked well for me.

Setting Up Azure Services

To send emails through Azure, I needed to set up two services: Email Communication Services and Communication Services. The Email Communication Services allow you to use an existing email domain or an Azure-provided domain for sending emails. While the Azure domain has a rather odd name with a GUID in it (great for testing), a custom domain offers a more professional look.

Since I didn’t have my own domain at this stage, I opted to use the Azure domain. Setting it up was straightforward. I just followed the wizard in the Azure portal.

Email Communication Services setup

Next, I created the Azure Communication Services resource. This acts as a communication hub where you can manage different types of communication like chat, SMS, and email. For emails, it integrates with the settings defined in the Email Communication Services.

Azure Communication Services setup

To connect your email domains, simply click “Connect your email domains” and fill out the form. The fields are mostly self-explanatory and guide you through the process without much hassle.

Connect email domain form

After that I went to the “Overview” page of my Azure Communication Services and clicked in “Click here to manage keys” to copy the “Connection String”. That is all for the azure part of the process.

Azure Communication Services connection string

Configuring Django

Once everything was set up on Azure, I moved on to configuring my Django application. First, I installed the django-azure-communication-email package:

pip install django-azure-communication-email

Then, I added these configurations to my settings.py file:

'''
Email - azure communication services - begin
'''
DEFAULT_FROM_EMAIL = os.environ['DEFAULT_FROM_EMAIL']
EMAIL_BACKEND = 'django_azure_communication_email.EmailBackend'
AZURE_COMMUNICATION_CONNECTION_STRING = os.environ['AZURE_COMMUNICATION_CONNECTION_STRING']

'''
Email - azure communication services - end
'''
  • DEFAULT_FROM_EMAIL – This is the sender’s email address. In my case, it was “doNotReply” plus the domain configured in the “Connect your email domains” form.
  • EMAIL_BACKEND – Set this to django_azure_communication_email.EmailBackend
  • AZURE_COMMUNICATION_CONNECTION_STRING – The connection string that was copied from the overview section of Azure Communication Services

As a reminder, it is a good practice to configure connection strings as environment variable.

With these configurations in place, my Django app was all set to handle email communications for sign-ups, password resets, and other workflows using Azure Communication Services.

Leave a Reply

Your email address will not be published. Required fields are marked *