Beispiel #1
0
        static bool ValidateEmailData(EmailData emailData)
        {
            if (emailData == null)
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(emailData.SenderName) ||
                string.IsNullOrWhiteSpace(emailData.SenderEmail) ||
                string.IsNullOrWhiteSpace(emailData.Subject) ||
                string.IsNullOrWhiteSpace(emailData.Message))
            {
                return(false);
            }

            try
            {
                var mailAddress = new MailAddress(emailData.SenderEmail, emailData.SenderName);
            }
            catch (FormatException)
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "POST")]
            HttpRequest request,
            ILogger log,
            CancellationToken cancellationToken)
        {
            log.LogInformation("SendEmail requested.");

            if (request.Body == null)
            {
                log.LogWarning("SendEmail without HTTP request body.");
                return(new BadRequestObjectResult("Expecting email data in the request body."));
            }

            EmailData emailData  = null;
            var       serializer = JsonSerializer.Create();

            using (var reader = new StreamReader(request.Body, Encoding.UTF8))
            {
                emailData = (EmailData)serializer.Deserialize(reader, typeof(EmailData));
            }

            if (!ValidateEmailData(emailData))
            {
                log.LogError("Send email failed: missing email data.");
                return(new BadRequestObjectResult("Missing email data: sender name, address, subject, or message."));
            }

            await SendGridEmail(emailData, cancellationToken);

            log.LogInformation("Email sent successfully from '{SenderName}'<{SenderEmail}>.", emailData.SenderName,
                               emailData.SenderEmail);

            return(new OkObjectResult("Email sent successfully."));
        }
Beispiel #3
0
        static Task SendGridEmail(EmailData emailData, CancellationToken cancellationToken)
        {
            var settingApiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");

            var email = new SendGridMessage {
                From = new EmailAddress(emailData.SenderEmail, emailData.SenderName)
            };

            email.AddTos(GetAddresses());
            email.SetFrom(new EmailAddress(emailData.SenderEmail, emailData.SenderName));
            email.Subject = emailData.Subject.Normalize(NormalizationForm.FormKD);
            var message =
                $"{emailData.Message}<br><br>{emailData.SenderName}<br>{emailData.SenderEmail}<br>{emailData.Telephone}";

            email.HtmlContent = message.Normalize(NormalizationForm.FormKD);

            var client = new SendGridClient(settingApiKey);

            return(client.SendEmailAsync(email, cancellationToken));
        }