Quiplogs Notifications (Email only. SMS support coming soon)
This project arose around a need to be able to send Notifications to an Azure queue and then have that queue be processed and sent off to a third party for porcessing (SendGrid).
This project produces TWO Nuget Packages.
- Quiplogs.Notifications.Send - The package takes an type of IEmail and send it to the Azure Queue
- Quiplogs.Notifications.Process - The package processes a queue item and sends it off to the SendGrid API
Requirements
- Azure Subscription with Azure storage to utilize the Queue
- SendGrid Account
Quiplogs.Notifications.Send
Installing & Configuring this package is done in 4 steps
-
Install the Quiplogs.Notifications.Send nuget package
-
Add configuration to appsettings.json
"AppSettings": {
"QuiplogsNotifications": {
"Azure": {
"DataConnectionString": "azure_connection_string",
"EmailQueueName": "queue_name"
},
"Security": {
"Key": "ecryption_key //e.g. b14ca5898a4e4133bbce2ea2315a1916"
}
}
- Register Autofac and pass in IConfiguration
IConfiguration Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
var builder = new ContainerBuilder();
builder.RegisterModule(new SendNotificationModule(Configuration));
var container = builder.Build();
- Create a email and send (This example uses the Templated Email from SendGrid)
var tags = new Dictionary<string, string>();
tags.Add("first_name", "first name");
tags.Add("last_name", "last name");
var email = new EmailWithTemplate
{
TemplateId = "SendGrid Template GUID",
FromEmailAddress = "from email",
FromName = "from name",
ToEmailAddress = "to email",
ToName = "to name",
Subject = "email subject",
ReplacementTags = tags
};
var sendService = container.Resolve<ISendService>();
sendService.SendNotification(email);
Quiplogs.Notification.Process
Installing & Configuring this package is done in 6 steps
-
Install nuget package Quiplogs.Notification.Process 1.0.1
-
Add Configuration Settings
- SENDGRID_API_KEY (Your generated SendGrid API Key)
- SECURITY_KEY (same security token you used in the "Send module" e.g. b14ca5898a4e4133bbce2ea2315a1916)
-
Reference the following packages
using AzureFunctions.Autofac;
using Quiplogs.Notifications.Process;
using Quiplogs.Notifications.Process.Interfaces;
- Add the following as an attribute to your Azure Queue function class
[DependencyInjectionConfig(typeof(ProcessNotificationModule))]
- Change Run() Signature
- inject ISendGridService
- change input from string to byte[] (email gets stored as byte[] in queue)
- change queue name to where you store emails
- set connection config variable
[QueueTrigger("set the queue name for your email", Connection = "set connection string key")] byte[] encryptedMail, [Inject] ISendGridService sendGridService, ILogger log)
- Call send grid service
sendGridService.SendMail(encryptedMail);
The function as a whole will look like the following example
using AzureFunctions.Autofac;
using Quiplogs.Notifications.Process;
using Quiplogs.Notifications.Process.Interfaces;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace FunctionApp1
{
[DependencyInjectionConfig(typeof(ProcessNotificationModule))]
public static class EmailQueueFunction
{
[FunctionName("EmailQueueFunction")]
public static void Run([QueueTrigger("twilioemailqueue", Connection = "AzureWebJobsStorage")] byte[] encryptedMail, [Inject] ISendGridService sendGridService, ILogger log)
{
sendGridService.SendMail(encryptedMail);
log.LogInformation($"Emailed sent");
}
}
}