Configuring email notifications in Azure SQL database

If you are a Database Administrator, you always need to know what is happening in the database. You need to be aware of the activities going on in the database.

Therefore, in order to monitor activities in the database, email notifications can help a lot. You can define a notification email that will be triggered when a certain action triggers in the database.

In an Azure SQL database, you can send email notifications using a SendGrid account. This article will help you to know how you can send email notifications with the help of a SendGrid account.

  • Create SendGrid account in Azure
  • Generate API key in SendGrid
  • Create a logic app in Azure

Create SendGrid account in Azure

This section will help you to create a SendGrid account in Microsoft Azure. Follow the below steps to create a SendGrid account:

  • Open your Azure portal and search for SendGrid in the search box.
  • Click on SendGrid Accounts.
Create sendgrid account in Azure
Azure portal
  • Click on Create to create a new SendGrid account.
  • Specfify the subscription to use, resource group, name of the account and your plan. You can click on Change plan to select the plan you want to use.
  • I am using the Free plan. You can select any plan depending on your requirements. Click on Review + subscribe.
Azure SQL database sendgrid
Specify the SendGrid account details
  • Review the configuration of the SendGrid account and click on Subscribe.
  • The subscription is created. However, the process is not complete yet.
  • In the Azure portal, search for your SendGrid account in the search box and click on it.
  • Then click on Open SaaS Account on publisher’s site to configure your SendGrid account. The SendGrid website will be opened in a new tab.
  • Firstly, you have to verify your account details. Also, you have to create a sender. I am selecting the Single Sender type.
  • Enter the details of the sender like email address that will be used to send the email. After entering the details, click on Create.
azure sql sendgrid
Create a sender
  • Once your have created the sender, you have to verify the sender email address.
  • After the verification yo can navigate to your dashboard and manage your SendGrid account.

Read: How to Create a Single Database in Azure SQL

Generate API key in SendGrid

The next step is to create an API key that you will use to authenticate to the SendGrid account while sending emails from the Azure SQL database.

  • To create an API key, navigate to Settings in the left side navigation bar and click on API Keys and then click on Create API Key.
Configuring email notifications in Azure SQL database
Create an API key
  • Specify the name for your API Key and select the access type according to your requirements. Click on Create & View.
Create API key in SendGrid
Create API Key
  • You will see the API key that you have generated. Copy that API Key in a safe place carefully.

Read: Pause and resume Azure SQL database

Create a logic app in Azure

The next step is to create a logic app in Azure. The logic app will help you to trigger an action. In our case, that action is to send an email using the SendGrid API. Follow the steps below to create the logic app:

  • In the Azure portal, search for Logic Apps and open it.
  • Click on Add to create a logic app.
  • In the Basics tab, specify the subscription to use, name of the resource group, the logic app name and the region. Click on Next : Hosting >.
send email from azure sql database
Creating a logic app
  • In the Hosting tab, select the azure storage account to use and the plans type. If you do not have one, you have to create it. You can click on Next to configure more options. At the end click on Create.
  • If you do not know how to create a storage account in Azure, you can read our article Backup and restore SQL Server to Azure Blob storage. We have defined in this article how you can create a storage account in Azure.
  • The next step is to create triggers and actions in the Azure logic app.
  • To create a trigger, open your logic app that you have created and click on Events in the left side navigation bar.
  • Click on Logic Apps.
how to Create API key in SendGrid
Logic App Events
  • Click on Templates and select the template named Blank Logic App.
how to send email from azure sql database
Select the Blank Logic App template
  • Search for SQL Server and select the trigger according to your requirements. I am selecting the When an item is created(v2). This trigger will fire an event when any new entry will be made in the spceified table in the database.
  • You have to make a connection with your database. Specify the necessary details about your database to make a connection. See the image below for reference:
Azure SQL database sendgrid
Create a connection with the database
  • Now you have to specify the table in the database in which when a record is created, an email will be sent. Also set the time frequency for the trigger.
  • For example, if you have set the time frequeny to 1 minute, the trigger will check for any new record in the table after every 1 minute and send the email if any new record is found in the table. Click on New step.
azure sql sendgrid
Select the table
  • Search for SendGrid in the search box and click on SendGrid.
  • In the SendGrid actions, select the Send email (v4) action.
how to Create sendgrid account in Azure
Select the action for sending an email
  • Specify the connection name of the connection to the Azure SQL database that we created earlier. In our example, it was TestCon. Also, specify the SendGrid API Key that you created earlier. Click on Create.
how to Create API key in SendGrid
Create the connection
  • Now you have to specify the email parameters like the sender’s email address, reciever’s email address, subject of the email and the body of the email.
  • In the body of the email, you can also select some dynamic content.
  • For example, you can create a column in the table to store HTML content and select this column in the email body. Now the HTML code will be sent to the sender.
  • Click on Save As to save the app. Specofy the name of the app and click on Create to create the app.
send email from azure sql database
Specifying the email parameters
  • Now if we insert a new row in the Student table, we will get an email on the specified email address.

You may also like to read the following articles on Azure SQL.

Thus, you might have learned how you can send emails from an Azure SQL database.

  • Create SendGrid account in Azure
  • Generate API key SendGrid
  • Create a logic app in Azure

Leave a Comment