2

Context: I currently send 10 separate HTML files markdown files to my audience, however, in the email it sends as different attachments as such in the image below: enter image description here

How can I package all these attachments in a single zip file instead individual attachments?

My Current Script to email out these attachments:

#Create vector of all sheetname that are in my excel paths
markdown_names <-  paste0("MVNDR","_",mvndr_nbr,".html")

path_markdown <- "C:path/Supplier_Ops_Parameterized_Reports/"

attachments_markdown <-  c(paste0(path_markdown, markdown_names))

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = paste("emails",
                         sep=";", collapse=NULL)
outMail[["subject"]] = "RMarkdown Report"
outMail[["body"]] = 
  "Hi -

email body


  "
purrr::map(attachments_markdown, ~ outMail[["attachments"]]$Add(.))
outMail$Send()
Jaskeil
  • 1,044
  • 12
  • 33

1 Answers1

0

You can use the following function :

library(RDCOMClient)
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])
    }
  }
}

You just have to give the paths to the files to send to the variable "vec_attachments".

Emmanuel Hamel
  • 1,769
  • 7
  • 19