-
Notifications
You must be signed in to change notification settings - Fork 625
/
ProcessEmailQueueMessage.cs
64 lines (55 loc) · 2.16 KB
/
ProcessEmailQueueMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using AllReady.Core.Notifications;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;
namespace NotificationProcessing
{
public static class ProcessEmailQueueMessage
{
[FunctionName("ProcessEmailQueueMessage")]
[StorageAccount("AzureWebJobsStorage")]
public static void Run([QueueTrigger("email-pending-deliveries")] string queueItem,
[SendGrid(ApiKey = "Authentication:SendGrid:ApiKey")] out Mail message, TraceWriter log)
{
var queuedEmailMessage = JsonConvert.DeserializeObject<QueuedEmailMessage>(queueItem);
var from = GuardAgainstInvalidEmailAddress(
Environment.GetEnvironmentVariable("Authentication:SendGrid:FromEmail"));
log.Info($"Sending email with subject `{queuedEmailMessage.Subject}` to `{queuedEmailMessage.Recipient}`");
message = new Mail
{
From = new Email(from, "AllReady"),
Subject = queuedEmailMessage.Subject
};
if (queuedEmailMessage.Message != null)
{
message.AddContent(new Content
{
Type = "text/plain",
Value = queuedEmailMessage.Message
});
}
if (queuedEmailMessage.HtmlMessage != null)
{
message.AddContent(new Content
{
Type = "text/html",
Value = queuedEmailMessage.HtmlMessage
});
}
var personalization = new Personalization();
personalization.AddTo(new Email(queuedEmailMessage.Recipient));
message.AddPersonalization(personalization);
}
private static string GuardAgainstInvalidEmailAddress(string @from)
{
if (string.IsNullOrWhiteSpace(@from))
{
throw new InvalidOperationException(
"Environment variable `Authentication:SendGrid:FromEmail` is missing or contains invalid entry.");
}
return @from;
}
}
}