public static async Task ProcessDeliveryStatusQueue( [QueueTrigger(DELIVERY_STATUS_QUEUE)] string deliveryStatusQueue, [Table(EMAIL_BLOCKED)] IAsyncCollector <EmailBlocked> tbEmailBlocked, [Table(EMAIL_TRACK)] CloudTable tbEmailTrack, int dequeueCount, ILogger log) { try { log.LogInformation($"New status to update. Dequeue count for this item: {dequeueCount}."); var deliveryStatusList = JsonConvert.DeserializeObject <List <DeliveryWebHook> >(deliveryStatusQueue); foreach (var status in deliveryStatusList) { if (DeliveryEvents.Contains(status.Event)) { if (BlockerEvents.Contains(status.Event)) { log.LogInformation($"Blocking email {status.Email}..."); await EmailBlocker.Create(tbEmailBlocked, status.Email, JsonConvert.SerializeObject(status), status.Event); } //TODO: Add to a list of Delivery Events await EmailTracker.Update(tbEmailTrack, status.Email, status.TrackerId, status.Event, log, status.SgMessageId); } else if (EngagementEvents.Contains(status.Event)) { //TODO: Add to a list of Engagement Events await EmailTracker.Update(tbEmailTrack, status.Email, status.TrackerId, status.Event, log, status.SgMessageId); } else { throw new Exception($"Event not mapped {status.Event}."); } } } catch (Exception ex) { log.LogError("An error has occurred: {0}", ex); throw; } }
public static async Task ProcessEmailQueue( [QueueTrigger(EMAIL_QUEUE)] OutgoingEmail emailQueue, [Table(EMAIL_TRACK)] CloudTable tbEmailTrack, [Table(EMAIL_BLOCKED)] CloudTable tbEmailBlocked, int dequeueCount, ILogger log) { log.LogInformation($"New email to send. Dequeue count for this message: {dequeueCount}."); try { var toEmail = emailQueue.ToAddress.Email; //Check whether the recipient of the message is blocked //TODO: Change this method to check if the email is blocked calling a SendGrid Api var recipientIsBlocked = await EmailBlocker.CheckIfBlocked(toEmail, tbEmailBlocked); if (recipientIsBlocked) { log.LogInformation($"Can't send email to {toEmail} because it has been blocked"); await EmailTracker.Update(tbEmailTrack, toEmail, emailQueue.TrackerId, Event.Blocked, log); return; } var response = await SendMail.SendSingleEmail(emailQueue.FromAddress, emailQueue.ToAddress, emailQueue.Subject, emailQueue.Body, emailQueue.TrackerId, log); if (!SuccessStatusCodes.Contains(response.StatusCode)) { throw new Exception($"Error sending mail. SendGrid response {response.StatusCode}"); } //Track that email request was sent await EmailTracker.Update(tbEmailTrack, toEmail, emailQueue.TrackerId, Event.SendRequested, log, response.MessageId); } catch (Exception ex) { log.LogError("An error has occurred: {0}", ex); throw; } }