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.
- 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.
- 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.
- 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.
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.
- Specify the name for your API Key and select the access type according to your requirements. Click on Create & View.
- You will see the API key that you have generated. Copy that API Key in a safe place carefully.
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 >.
- 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.
- Click on Templates and select the template named Blank Logic App.
- 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:
- 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.
- Search for SendGrid in the search box and click on SendGrid.
- In the SendGrid actions, select the Send email (v4) action.
- 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.
- 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.
- 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.
- How to Connect to Azure SQL database
- Reset Password Azure SQL database
- How to create table in Azure SQL
- How to rename a database in 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
Bijay is a Microsoft MVP since 2014 (7 times+) specialized in SharePoint. He is having more than 14 years of experience in SharePoint and other Microsoft technologies. Bijay is an entrepreneur, a technical blogger, and passionate about driving, wrote tons of articles in his own blogs EnjoySharePoint.com and SPGuides.com, and PythonGuides.com focuses on speaking activities for various user groups. He is the co-founder of TSInfo Technologies, a SharePoint Consulting, Outsourcing, Development, and training company.
During the long 14+ years of professional career, I have used SQL server in lots of projects. Enjoy my SQL Server tutorials.