I would like a code to read dataframe (df). Thereafter, send email in Microsoft Outlook, by looping through Email list, create a subset of the list by distinct Email, then email this list in HTML table format to email recipient.
df =
structure(list(Sponsor.Name = c("X", "Z", "MP"), Team.Name = c("Y",
"P", "HK"), Email = c("x@gmail.com", "y@gmail.com", "x@gmail.com"
), Amount = c(10, 20, 100)), row.names = c(NA, 3L), class = "data.frame")
For example; "x@gmail.com", will have a HTML output table of two entries.
I have found the following useful:
- How to show an excel worksheet in outlook body by R
- For loop on data frame with multiple columns to send mails in R
but I am unable to replicate it for my use.
Look forward to your assistance.
library(RDCOMClient)
library(openxlsx)
library(xtable)
library(dplyr)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@gmail.com" #insert real email address here
outMail[["subject"]] = paste0("Report ", Sys.Date() - 1)
df<-read.xlsx("email_test.xlsx")
emails<-df %>% distinct(df$Email) #read df file
split_emails<-split(df, emails)
iwalk(split_emails, ~print(xtable(df), type="html", print.results=FALSE)) #doesnot work
Thank you