-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSendRazorTemplateEmail.cs
56 lines (47 loc) · 1.96 KB
/
SendRazorTemplateEmail.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
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;
using SendGrid;
using System.Threading;
using AzureFunctionsRazorEmailTemplateSample.Views;
namespace AzureFunctionsRazorEmailTemplateSample.Function
{
public class SendRazorTemplateEmail
{
private readonly RazorViewToStringRenderer razorViewToStringRenderer;
public SendRazorTemplateEmail(RazorViewToStringRenderer razorViewToStringRenderer)
{
this.razorViewToStringRenderer = razorViewToStringRenderer;
}
[FunctionName("SendRazorTemplateEmail")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[SendGrid] IAsyncCollector<SendGridMessage> messageCollector,
ILogger log, CancellationToken cancellationToken)
{
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
var message = new SendGridMessage();
message.SetFrom("from@example.com");
message.AddTo("to@example.com");
message.SetSubject("Subject");
var model = new SampleEmailTemplateModel()
{
Name = name
};
var htmlContent = await razorViewToStringRenderer.RenderViewToStringAsync(model);
message.AddContent(MimeType.Html, htmlContent);
await messageCollector.AddAsync(message, cancellationToken);
log.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult(htmlContent);
}
}
}