public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); ContactFormCommand command = JsonConvert.DeserializeObject <ContactFormCommand>(requestBody); ResolveDependencies(log); _logger.LogInformation("Validating command"); var validatorResult = _validator.Validate(command); if (!validatorResult.IsValid) { return(new BadRequestObjectResult(new CommandResult() { Success = false, Message = "Invalid command. See errors for more info.", Errors = validatorResult.Errors.Select(e => e.ErrorMessage).ToList() })); } await _emailService.SendMail(command); return(new OkResult()); }
public async Task SendMail(ContactFormCommand command) { var client = new SendGridClient(_apiKey); var from = new EmailAddress(command.Email, command.Name); var subject = "New message from your contact form"; var to = new EmailAddress(_email, _userName); var plainTextContent = command.Message; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, string.Empty); await client.SendEmailAsync(msg); }