Integrating SendGrid Service Via Azure Functions

Introduction

 
This article is in continuation of my previous article where I have explained how to perform Azure blob storage binding for Azure Functions. Here, we are going to discuss how to integrate the SendGrid service to Azure Functions. So, in my last article, we ended up storing the invoice in a blob. Now, we are going to see how to send the file as an attachment and send it to the respective users via an email. 
 
Prerequisites
 
Basic knowledge of Azure Functions and Azure Storage Service
 

Integrate Azure Blob to Azure Functions

In my last article, we created a function GenerateInvoice which is integrated with the Azure Blob Storage, and stores the invoice details as a text file. In this article, we are going to add a trigger to this function to send an email.

Step 1
 
Go to the GenerateInvoice function and select "Integrate", as shown in the below figure.
 
Figure 1
 
Step 2
 
Select the Azure Blob output and click on "Go" to create a new function to send an email based on the Azure blob output trigger.
 
 
Step 3
 
Select "Azure Blob Storage trigger" template from the available templates.
 
Step 4
 
Now, we will get a form. Create a new function, name the function, provide blob path in path details. In my case, it is invoicedetails/{filename}. Now, give a storage account which we used in the previous function.
 
Step 5
 
Click the "Create" button. It will create a new function called SendInvoiceEmail. Replace the existing code with the code given below.
  1. #r "SendGrid"  
  2.   
  3. using System;  
  4. using System.Text.RegularExpressions;  
  5.   
  6. public static void Run(string myBlob, string filename, ILogger log)  
  7. {  
  8.    log.LogInformation(myBlob);  
  9.     var email=Regex.Match(myBlob,@"^Email\:\ (.+)$", RegexOptions.Multiline).Groups[1].Value;  
  10.     log.LogInformation($"Send invoice to {email} with filename :{filename}");  

From the above code, you can observe that we are getting the data from the invoice detail file which is stored in the blob service as a string and the file name also is a string.
 
We are using a regular expression to get the Email address of the user from the string myBlob. Remember to include the namespace System.Text.RegularExpression in your script.

Step 6
 
Let’s trigger the function using POSTMAN, for testing.
 
 
 
 
From the above figure, you can see the log.

Step 7
 
Now, we need to send the order details as an attachment to the user via an Email. Let’s implement a SendGrid Service as an output of the trigger. Click on "Integrate".
 
 
 
Step 8
 
In the "Integrate" window, click on new output and select the SendGrid template.
 
 
Step 9
 
In SendGrid output form, provide the SendGrid API key which is stored in App setting available in platform features, as shown in the below figure.

If you don’t have an API key, get a free trial of SendGrid and explore it.
Go to Application Settings,
 
 
Add "SendGridAPI Key".
 
 
Save the SendGrid output form by providing the API Key APP setting and saving it.

Step 10
 
Replace the existing code of SendVoiceEmail with the code written below.
  1. #r "SendGrid"  
  2. using System;  
  3. using SendGrid.Helpers.Mail;  
  4. using System.Text.RegularExpressions;  
  5.   
  6. public static void Run(string myBlob, string filename, ILogger log,  out SendGridMessage message)  
  7. {  
  8.    log.LogInformation(myBlob);  
  9.     var email=Regex.Match(myBlob,@"^Email\:\ (.+)$", RegexOptions.Multiline).Groups[1].Value;  
  10.     log.LogInformation($"Send invoice to {email} with filename :{filename}");  
  11.   
  12.      message = new SendGridMessage();  
  13.     message.AddTo(new EmailAddress(email));  
  14.     var base64Content = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(myBlob));  
  15.   
  16.     message.AddAttachment(  
  17.         "Invoice.txt",  
  18.         base64Content,  
  19.         "text/plain",  
  20.         "attachment",  
  21.         "Invoice"  
  22.     );  
  23.   
  24.     message.AddContent("text/html""Hi, PFA of your Invoice");  
  25.     message.Subject = "Thanks for your order";  
  26.     message.From = new EmailAddress("[email protected]");  

Step 10

Let’s trigger the function using POSTMAN.
 
 
 
An email is sent to the user inbox, with the attachment.
 
 
 

Conclusion


We have seen how to bind the SendGrid Service to the Azure Functions to send an email to the user when the function triggers. We will see more about Azure Functions in my future articles.