Skip to content

Commit 06d385b

Browse files
authored
Merge pull request #257 from fvandillen/sendgrid-template-extension
Add support for SendGrid dynamic templates.
2 parents 539e017 + f192fa2 commit 06d385b

File tree

4 files changed

+111
-8
lines changed

4 files changed

+111
-8
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using FluentEmail.Core;
2+
using FluentEmail.Core.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace FluentEmail.SendGrid
9+
{
10+
public static class IFluentEmailExtensions
11+
{
12+
public static async Task<SendResponse> SendWithTemplateAsync(this IFluentEmail email, string templateId, object templateData)
13+
{
14+
var sendGridSender = email.Sender as ISendGridSender;
15+
return await sendGridSender.SendWithTemplateAsync(email, templateId, templateData);
16+
}
17+
}
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using FluentEmail.Core;
2+
using FluentEmail.Core.Interfaces;
3+
using FluentEmail.Core.Models;
4+
using SendGrid;
5+
using SendGrid.Helpers.Mail;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
12+
namespace FluentEmail.SendGrid
13+
{
14+
public interface ISendGridSender : ISender
15+
{
16+
/// <summary>
17+
/// SendGrid specific extension method that allows you to use a template instead of a message body.
18+
/// For more information, see: https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/.
19+
/// </summary>
20+
/// <param name="email">Fluent email.</param>
21+
/// <param name="templateId">SendGrid template ID.</param>
22+
/// <param name="templateData">SendGrid template data.</param>
23+
/// <param name="token">Optional cancellation token.</param>
24+
/// <returns>A SendResponse object.</returns>
25+
Task<SendResponse> SendWithTemplateAsync(IFluentEmail email, string templateId, object templateData, CancellationToken? token = null);
26+
}
27+
}

src/Senders/FluentEmail.SendGrid/SendGridSender.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace FluentEmail.SendGrid
1515
{
16-
public class SendGridSender : ISender
16+
public class SendGridSender : ISendGridSender
1717
{
1818
private readonly string _apiKey;
1919
private readonly bool _sandBoxMode;
@@ -30,8 +30,41 @@ public SendResponse Send(IFluentEmail email, CancellationToken? token = null)
3030

3131
public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken? token = null)
3232
{
33-
var sendGridClient = new SendGridClient(_apiKey);
33+
var mailMessage = await BuildSendGridMessage(email);
34+
35+
if (email.Data.IsHtml)
36+
{
37+
mailMessage.HtmlContent = email.Data.Body;
38+
}
39+
else
40+
{
41+
mailMessage.PlainTextContent = email.Data.Body;
42+
}
43+
44+
if (!string.IsNullOrEmpty(email.Data.PlaintextAlternativeBody))
45+
{
46+
mailMessage.PlainTextContent = email.Data.PlaintextAlternativeBody;
47+
}
48+
49+
var sendResponse = await SendViaSendGrid(mailMessage, token);
50+
51+
return sendResponse;
52+
}
3453

54+
public async Task<SendResponse> SendWithTemplateAsync(IFluentEmail email, string templateId, object templateData, CancellationToken? token = null)
55+
{
56+
var mailMessage = await BuildSendGridMessage(email);
57+
58+
mailMessage.SetTemplateId(templateId);
59+
mailMessage.SetTemplateData(templateData);
60+
61+
var sendResponse = await SendViaSendGrid(mailMessage, token);
62+
63+
return sendResponse;
64+
}
65+
66+
private async Task<SendGridMessage> BuildSendGridMessage(IFluentEmail email)
67+
{
3568
var mailMessage = new SendGridMessage();
3669
mailMessage.SetSandBoxMode(_sandBoxMode);
3770

@@ -57,7 +90,10 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
5790
mailMessage.AddHeaders(email.Data.Headers.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
5891
}
5992

60-
mailMessage.Categories = email.Data.Tags.ToList();
93+
if(email.Data.Tags != null && email.Data.Tags.Any())
94+
{
95+
mailMessage.Categories = email.Data.Tags.ToList();
96+
}
6197

6298
if (email.Data.IsHtml)
6399
{
@@ -94,11 +130,6 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
94130
break;
95131
}
96132

97-
if (!string.IsNullOrEmpty(email.Data.PlaintextAlternativeBody))
98-
{
99-
mailMessage.PlainTextContent = email.Data.PlaintextAlternativeBody;
100-
}
101-
102133
if (email.Data.Attachments.Any())
103134
{
104135
foreach (var attachment in email.Data.Attachments)
@@ -109,6 +140,12 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
109140
}
110141
}
111142

143+
return mailMessage;
144+
}
145+
146+
private async Task<SendResponse> SendViaSendGrid(SendGridMessage mailMessage, CancellationToken? token = null)
147+
{
148+
var sendGridClient = new SendGridClient(_apiKey);
112149
var sendGridResponse = await sendGridClient.SendEmailAsync(mailMessage, token.GetValueOrDefault());
113150

114151
var sendResponse = new SendResponse();

test/FluentEmail.Core.Tests/SendGridSenderTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ public async Task CanSendEmail()
4242
Assert.IsTrue(response.Successful);
4343
}
4444

45+
[Test, Ignore("No sendgrid credentials")]
46+
public async Task CanSendTemplateEmail()
47+
{
48+
const string subject = "SendMail Test";
49+
const string templateId = "123456-insert-your-own-id-here";
50+
object templateData = new
51+
{
52+
Name = toName,
53+
ArbitraryValue = "The quick brown fox jumps over the lazy dog."
54+
};
55+
56+
var email = Email
57+
.From(fromEmail, fromName)
58+
.To(toEmail, toName)
59+
.Subject(subject);
60+
61+
var response = await email.SendWithTemplateAsync(templateId, templateData);
62+
63+
Assert.IsTrue(response.Successful);
64+
}
65+
4566
[Test, Ignore("No sendgrid credentials")]
4667
public async Task CanSendEmailWithReplyTo()
4768
{

0 commit comments

Comments
 (0)