-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailer.cs
51 lines (44 loc) · 1.64 KB
/
Mailer.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
using Postmark.Model.Suppressions;
using PostmarkDotNet;
namespace NewsletterBuilder;
public class Mailer
{
private static PostmarkClient _client;
private static bool _isDevelopment;
public static void Configure(string postmarkServerToken, bool isDevelopment)
{
_client = new PostmarkClient(postmarkServerToken);
_isDevelopment = isDevelopment;
}
private readonly List<PostmarkMessage> _messages = [];
private int _totalMessages;
public void Enqueue(string to, string subject, string from, string replyTo, bool isBroadcast, string html, string plainText = null)
{
if (_isDevelopment && ++_totalMessages > 2) return;
if (_messages.Count >= 500) throw new InvalidOperationException("Too many messages queued");
_messages.Add(new PostmarkMessage
{
To = _isDevelopment ? replyTo : to,
From = from,
ReplyTo = replyTo,
Subject = subject,
HtmlBody = html,
TextBody = plainText,
MessageStream = isBroadcast ? "broadcast" : "outbound",
Tag = $"{(isBroadcast ? "Newsletter" : "Reminder")}{(_isDevelopment ? " Test" : string.Empty)}",
TrackOpens = false,
TrackLinks = LinkTrackingOptions.None
});
}
public async Task SendAsync()
{
if (_messages.Count == 0) return;
await _client.SendMessagesAsync(_messages);
_messages.Clear();
}
public static async Task<HashSet<string>> GetSuppressedRecipientsAsync()
{
var suppressionResponse = await _client.ListSuppressions(new PostmarkSuppressionQuery(), "broadcast");
return suppressionResponse.Suppressions.Select(o => o.EmailAddress.ToLowerInvariant()).ToHashSet(StringComparer.OrdinalIgnoreCase);
}
}