MSM
MSM

Reputation: 69

How to loop though a dataframe of mail-ids and send mail using RDCOMClient

I need to send mail to all email ids in a column of the data frame. The column values are dynamically changing. I have used FOR loop for this but end up receiving the below warning and it sends mail only to 1st mail id in the column.

Datacheck=(Data=c("[email protected]","[email protected]")

for (mail in Datacheck$Data) 
{
  
  outApp <- COMCreate("Outlook.Application")
  outMail = outApp$CreateItem(0)
  
  outMail[["To"]] = mail
  outMail[["subject"]] = "Subject"
  #outMail[["body"]] = paste(" Hi" "\n \n checkk.")
  
  outMail$Send()
  
  Sys.sleep(0.5)
  
  if (mail == Datacheck$Data[-1]) cat("Done!")
}

Warning
1: In if (mail == Datacheck$Data[-1]) cat("Done!") :
  the condition has length > 1 and only the first element will be used
2: In if (mail == Datacheck$Data[-1]) cat("Done!") :
  the condition has length > 1 and only the first element will be used

Upvotes: 0

Views: 123

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49453

First of all, there is no need to create a new Outlook Application instance in the loop each time:

outApp <- COMCreate("Outlook.Application")

for (mail in Datacheck$Data) 
{
   outMail = outApp$CreateItem(0)
  
  outMail[["To"]] = mail
  outMail[["subject"]] = "Subject"
  #outMail[["body"]] = paste(" Hi" "\n \n checkk.")
  
  outMail$Send()
}

Try to debug the code line-by-line to make sure the loop is working correctly.

Upvotes: 1

Related Questions