Send emails automatically through a script in R every day at the same time

I will set up a script to obtain meteorological data and I wanted to send this follow-up by email every day, at the same time. I'm thinking of renting a machine on AWS and letting an R script run indefinitely. Is there a better way that is not so expensive?

For this problem, I would just like an opinion/tips of possible ways.

Thanks

Upvotes: 0

Views: 767

Answers (1)

Emmanuel Hamel
Emmanuel Hamel

Reputation: 2233

You can use the outlook software with the R package RDCOMClient. Here is a function that allows the user to send emails with outlook from R automatically :

send_email <- function(vec_to = "",
                       vec_cc = "",
                       vec_bcc = "",
                       char_subject = "",
                       char_body = "",
                       char_htmlbody = "",
                       vec_attachments = "") {

  Outlook <- RDCOMClient::COMCreate("Outlook.Application")
  Email <- Outlook$CreateItem(0)
  Email[["to"]] <- vec_to
  Email[["cc"]] <- vec_cc
  Email[["bcc"]] <- vec_bcc
  Email[["subject"]] <- char_subject

  if (char_body != "" && char_htmlbody != "") {
    stop("Error")
  }

  if (char_htmlbody == "") {
    Email[["body"]] <- char_body
  } else {
    Email[["htmlbody"]] <- char_htmlbody
  }

  if (vec_attachments[1] != "") {
    for (i in seq_along(vec_attachments)) {
      Email[["attachments"]]$Add(vec_attachments[i])
    }
  }

  Email$Send()
}

Upvotes: 1

Related Questions