SendGrid is one of the popular services that allow the audience to send emails for different purposes. In today’s tutorial we will explore to:
- Create SendGrid account
- Generate SendGrid API Key
- Configure SendGrid API key with Azure function app
Send an email notification to the website administrator
Here, we will learn how to create a SendGrid output binding and send an email notification to the administrator with a static content. In general there would be only administrators so we will be hard coding the email address of the administrator in the To address field of the SendGrid output binding
Getting ready
- Create a SendGrid account API Key from the Azure Management Portal.
- Generate an API Key from the SendGrid Portal.
Create a SendGrid account
- Navigate to Azure Management Portal and create a SendGrid Email Delivery account by searching for the same in the Marketplace shown as follows:
- In the SendGrid Email Delivery blade, click on Create button to navigate to the Create a new SendGrid Account. Please select Free tier in the Pricing tier and provide all other details and click on the Create button shown as follows:
- Once the account is created successfully, navigate to the SendGrid account. You can use the search box available in the top which is shown as follows:
- Navigate to the Settings, choose configurations and grab the username and SmtpServer from the Configurations blade.
Generate SendGrid API key
- In order to utilize SendGrid account by the Azure Functions runtime, we need to provide the SendGrid API key as input to the Azure Functions. You can generate an API Key from the SendGrid portal. Let's navigate to the SendGrid portal by clicking on the Manage button in the Essentials blade of the SendGrid account shown as follows:
- In the SendGrid portal, click on the API Keys under Settings section of the Left hand side menu shown as follows:
- In the API Keys page, click on Create API Key shown as follows:
- In the Create API Key popup, provide a name and choose the API Key Permissions and click on Create & View button.
- After a moment you will be able to see the API key. Click on the key to copy the same to the clipboard:
Configure SendGrid API key with Azure Function app
- Create a new app setting in the Azure Function app by navigating to the Application Settings blade under the Platform features section of the function app shown as follows:
- Click on Save button after adding the app settings in the preceding step.
How to do it...
- Navigate to the Integrate tab of the RegisterUser function and click on New Output button to add a new output binding.
- Choose the SendGrid output binding and click on Select button to add the binding.
- Please provide the following parameters in the SendGrid output binding:
- Message parameter name - leave the default value - message. We will be using this parameter in the run method in a moment.
Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at R$50/month. Cancel anytime
- SendGrid API key: Please provide the app settings key that you have created in the application settings.
- To address: Please provide the email address of the administrator.
- From address: Please provide the email address from where you would like to send the email. In general, it would be kind of donotreply@example.com.
- Message subject: Please provide the subject that you would like to have in the email subject.
- Message Text: Please provide the email body text that you would like to have in the email body.
- Below is how the SendGrid output binding should look like after providing all the fields:
- Once you review the values, click on Save to save the changes.
- Navigate to Run method and make the following changes:
- Add a new reference for SendGrid and also the namespace
- Add a new out parameter message of type Mail.
- Create an object of type Mail.
- Following is the complete code of the Run method:
#r "Microsoft.WindowsAzure.Storage"
#r "SendGrid"
using System.Net;
using SendGrid.Helpers.Mail;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
public static void Run(HttpRequestMessage req, TraceWriter log,
CloudTable objUserProfileTable,
out string objUserProfileQueueItem,
out Mail message
)
{
var inputs = req.Content.ReadAsStringAsync().Result;
dynamic inputJson = JsonConvert.DeserializeObject<dynamic>(inputs);
string firstname= inputJson.firstname;
string lastname=inputJson.lastname;
string profilePicUrl = inputJson.ProfilePicUrl;
objUserProfileQueueItem = profilePicUrl;
UserProfile objUserProfile = new UserProfile(firstname, lastname); TableOperation objTblOperationInsert =
TableOperation.Insert(objUserProfile);
objUserProfileTable.Execute(objTblOperationInsert);
message = new Mail();
}
public class UserProfile : TableEntity
{
public UserProfile(string lastName, string firstname,string profilePicUrl)
{
this.PartitionKey = "p1";
this.RowKey = Guid.NewGuid().ToString();;
this.FirstName = firstName; this.LastName = lastName; this.ProfilePicUrl = profilePicUrl;
}
public UserProfile() { }
public string FirstName { get; set; } public string LastName { get; set; } public string ProfilePicUrl {get; set;}
}
- Now, let's test the functionality of sending the email by navigating to the RegisterUser function and submit a request with the some test values:
{
"firstname": "Bill", "lastname": "Gates",
"ProfilePicUrl":"https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/ Bill_Gates_June_2015.jpg/220px-Bill_Gates_June_2015.jpg"
}
How it works...
The aim here is to send a notification via email to an administrator updating that a new registration got created successfully.
We have used the one of the Azure Function experimental templates named SendGrid as a SMTP server for sending the emails by hard coding the following properties in the SendGrid output bindings:
- From email address
- To email address
- Subject of the email
- Body of the email
SendGrid output bindings will use the API key provided in the app settings to invoke the required APIs of the SendGrid library for sending the emails.
To summarize, we learnt about sending an email notification using SendGrid service.
[box type="shadow" align="" class="" width=""]This article is an excerpt from the book, Azure Serverless Computing Cookbook, written by Praveen Kumar Sriram. It contains over 50 recipes to help you build applications hosted on Serverless architecture using Azure Functions.[/box]
5 reasons why your business should adopt cloud computing