Outlook Notification Emails

Some projects I work on have reports or tasks that are automated in R and scheduled to run on a regular basis. However, some of these tasks may encounter an error from time to time. Perhaps the taks encountered an edge case and caused the script to crash. In R, it is relatively easy to configure the script to send an email when a user-defined condition is met to promptly address these situations. The following code chunk accomplishes this for those with a Microsoft Outlook based email system.

condition_met <- "Yes"

if (condition_met == "Yes") {
  # If there are duplicates, notify by email
  library(Microsoft365R)

  # Authenticate and create Outlook object
  outlook <- get_business_outlook()  # or get_personal_outlook()

  # Create email
  email <- outlook$create_email(
    body = "Condition was met in automated workflow. Review and revise code for data QA check.",
    subject = "Condition met in automted workflow",
    to = "outlook-exchange-email@organization.edu"
  )

  # Send email
  email$send()

  # Stop the render
  stop("Error in .qmd file. Condition was met")
}