-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendPortfolioRequest.cs
52 lines (44 loc) · 2.3 KB
/
SendPortfolioRequest.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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;
using System.IO;
using System.Threading.Tasks;
namespace emailsender
{
public class SendPortfolioRequest
{
[FunctionName("SendPortfolieEmail")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "SendEmail/portfolio")] HttpRequest req,
ILogger log,
[SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] IAsyncCollector<SendGridMessage> messageCollector)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// requestBody contains email
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var form = JsonConvert.DeserializeObject<Form>(requestBody);
var targetEmailAddress = System.Environment.GetEnvironmentVariable("TargetEmailAddress");
if (string.IsNullOrEmpty(targetEmailAddress))
{
targetEmailAddress = "[email protected]";
}
var message = new SendGridMessage();
message.AddTo(new EmailAddress(targetEmailAddress));
message.SetFrom(new EmailAddress(form.From));
message.SetSubject($"Portfolio Request received!");
message.AddContent("text/plain", $"Hi there,\n \nThank for your interest in our portfolio!\nWe successfully received your request, and will get back to you shortly.\n \nBest wishes,\nThe Team");
await messageCollector.AddAsync(message);
var responseMessage = new SendGridMessage();
responseMessage.AddTo(new EmailAddress(form.From));
responseMessage.SetFrom(new EmailAddress(targetEmailAddress));
responseMessage.SetSubject($"Portfolio Request received!");
responseMessage.AddContent("text/plain", $"Hi there,\n \nThank for your interest in our portfolio!\nWe successfully received your request, and will get back to you shortly.\n \nBest wishes,\nThe Team");
await messageCollector.AddAsync(responseMessage);
return new OkObjectResult("Emails sent.");
}
}
}