Created
March 25, 2017 10:00
-
-
Save JoachimL/8577a9d7e89455b6fcf4ddfca2953385 to your computer and use it in GitHub Desktop.
Sendgrid through azure function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "SendGrid" | |
using System.Net; | |
using SendGrid.Helpers.Mail; | |
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Mail message) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
// parse query parameter | |
string name = req.GetQueryNameValuePairs() | |
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) | |
.Value; | |
// Get request body | |
dynamic data = req.Content.ReadAsAsync<object>().Result; | |
// Set name to query string or body data | |
name = name ?? data?.name; | |
message = new Mail(); | |
message.Subject = "Someone passed a name to an azure function!"; | |
var personalization = new Personalization(); | |
personalization.AddTo(new Email("[email protected]")); | |
Content content = new Content | |
{ | |
Type = "text/plain", | |
Value =$"The name was {name}" | |
}; | |
message.AddContent(content); | |
message.AddPersonalization(personalization); | |
return name == null | |
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body") | |
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment