How to send emails with Azure Communication Services

2024-08-13

Introduction

There are multiple email sending services like SendGrid, EmailOctopus, postmark etc.

All of them are great, and usually has good free tier and roughly $10 per month for 10k emails.

But if you are already using Azure for some reason or if you like to write your own implementations, you can use Azure Commuication Service + Azure email service.

The goal of this article is to document what I had to do to set it up, so I can do it again next time. Because it wasn't as easy as I thought it would be.

Azure Communication Service

Azure Communication Services is a fully managed platform that supports text, voice, video and emails.

Email Communication Service is a part of Azure Communication Services.

How to set it up

Pre requisites

  1. go to Azure portal (https://portal.azure.com/)
  2. assuming you have Azure account, subscription, and resource group.

Create Email Communication Service

First I prefer to start with Email Communication service because it's most time consuming part.

create new resource

  1. create new resource in Azure portal.
  2. search for "Communication Services" and pick "Email Communication Service".
  3. pick any descriptive (to yourself) name, and click "Create". Create Email Communication Service

Add custom domain

  1. open your new resource
  2. click "Setup custom domain"

Setup domain

  1. choose your email domain. I used a subdomain of my domain, like notifications.devtoolsdaily.com Add custom domain

Verify custom domain

  1. verify your domain. You need to add some DNS records to your domain. Azure will give you the values. Verify domain
  2. paste the values in your DNS provider:

Here is an example for Netlify: Verify domain netlify

And here is porkbun: Verify domain

Configurate SPF and DKIM records

These are important for email deliverability.

  1. Azure will tell you which values to put for SPF SPF domain

  2. DKIM IMPORTANT - add your subdomain e.g Azure tells you to add

selector1-azurecomm-prod-net._domainkey

but you want to add

selector1-azurecomm-prod-net._domainkey.notifications

as a CNAME record DKIM record

  1. wait for Azure verification and DNS propagation. Domain verified

Create MailFrom address

By default Azure provides with DoNotReply email address. which isn't even customizable. (at least at the moment of writing on Aug 14 2024)

UI has disabled "Create" button for custom MailFrom address.

MailFrom disabled

but management via azure cli is possible.

az communication email domain sender-username create \
  --domain-name notifications.devtoolsdaily.com  \
  --email-service-name devtoolsdaily-email \
  --resource-group <Your group> \
  --subscription <Your subscription> \
  --display-name "DevToolsDaily team" \
  --name "no-reply" \
  --username "no-reply"

Now we need to create Communication Service

  1. same as before, create new resource in Azure portal. but now with type "Communication Services"
  2. give it a name, e.g "devtoolsdaily-comms" and click "Create"
  3. go to resource

Connect Domain to Communication service

  1. scroll down to "Email" section in the left menu.
  2. click "Domains"
  3. click "Connect domain"

Connect domain

Send email from Azure portal

  1. open try email
  2. pick correct doman and reply-to address

Send email

To be continued...

In Part 2 we will write a simple Node.js script to send emails using Azure Communication Service.