R/send_gmailr.R

Defines functions send_gmailr

send_gmailr <- function(from, to, subject, body) {
  # Send email with 'gmailr' package wrapped in a single function
  #
  # Arguments:
  #   from {char} -- from email address
  #   to {char} -- to email address
  #   subject {char} -- email subject
  #   body {char} -- email body
  #
  # Returns:
  #   nothing
  #
  # Parameter examples:
  #   to = "aksooklaris@ucdavis.edu"
  #   from = "Andoni Sooklaris <andoni.sooklaris@gmail.com>"
  #   body = "This is a test email sent with gmailr"
  #   subject = "Send Email with Gmailr!"

  library(gmailr)
  library(purrr)

  stopifnot(is.character(from))
  stopifnot(is.character(to))
  stopifnot(is.character(body))
  stopifnot(is.character(subject))

  # Create MIME message object
  message = gmailr::mime() %>%
    gmailr::from(from) %>%
    gmailr::to(to) %>%
    gmailr::text_body(body) %>%
    gmailr::subject(subject)

  # Send message
  gmailr::send_message(mail = message)
}
tsouchlarakis/rdoni documentation built on Sept. 16, 2019, 8:53 p.m.