R/emate.R

Defines functions emate_dry emate

Documented in emate

emate_dry <- function(email_content = NULL,
                      ind_verbose = FALSE, email_from = NULL,
                      email_to = NULL, email_cc = NULL, email_bcc = NULL,
                      email_replyto = NULL, email_subject = NULL,
                      email_header = "#markup: markdown",
                      emate_path = NULL, ind_sendnow = FALSE){
  # Sanity checks
  if (is.null(email_content))
    rlang::abort("The content of the email must not be null")
  if (is.null(email_from))
    rlang::abort("The email from field must not be null")
  if (is.null(email_to) & is.null(email_cc) & is.null(email_bcc))
    rlang::abort("At least one of the 'To', 'CC' or 'BCC' fields must not be null")
  if (is.null(email_subject))
    rlang::abort("The email subject must not be null")
  if (is.null(emate_path))
    rlang::abort("The path to the emate binary must not be null")

  # Construct command
  paste0(
    glue::glue("echo \"{email_content}\" | "),
    emate_path, " mailto ",
    dplyr::if_else(ind_verbose, "--verbose", ""),
    " ",
    add_nonull("--from", email_from),
    add_nonull("--to", email_to),
    add_nonull("--cc", email_cc),
    add_nonull("--bcc", email_bcc),
    add_nonull("--replyto", email_replyto),
    add_nonull("--subject", email_subject),
    add_nonull("--header", email_header),
    dplyr::if_else(ind_sendnow, "--send-now", "")
  )
}

#' Emate
#'
#' Run the emate command line interface to MailMate
#'
#' @param email_content content of email as a string
#' @param ind_verbose indicator - whether or not the command line output should be verbose
#' @param email_from the email address to send from
#' @param email_to the email address to send to
#' @param email_cc the email address to CC
#' @param email_bcc the email address to BCC
#' @param email_replyto the email address that responders will reply to, by default
#' @param email_subject the subject line for the email
#' @param email_header header information used by MailMate. Defaults to markdown parsing
#' @param emate_path the path on your local machine to the emate binary
#' @param ind_sendnow indicator - whether or not to send the email automatically
#' @param ind_dry indicator - whether or not to do a dry run
#' @export

emate <- function(email_content = NULL,
                  ind_verbose = FALSE, email_from = NULL,
                  email_to = NULL, email_cc = NULL, email_bcc = NULL,
                  email_replyto = NULL, email_subject = NULL,
                  email_header = "#markup: markdown",
                  emate_path = NULL, ind_sendnow = FALSE, ind_dry = FALSE){

  # Create command
  system_command <- emate_dry(email_content = email_content,
                              ind_verbose = ind_verbose,
                              email_from = email_from,
                              email_to = email_to,
                              email_cc = email_cc,
                              email_bcc = email_bcc,
                              email_replyto = email_replyto,
                              email_subject = email_subject,
                              email_header = email_header,
                              emate_path = emate_path,
                              ind_sendnow = ind_sendnow)

  # If dry, just output the command
  if (ind_dry) {
    cat("Dry run. Command that would be executed:")
    cat("\n")
    cat(system_command)
    # Otherwise, run the command
  } else {
    system(system_command)
  }

  return(invisible(NULL))
}
andrewjpfeiffer/mailmatr documentation built on Nov. 2, 2019, 1:52 p.m.