0

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("mess@xyz.com","ghyh@xyz.com")

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
MSM
  • 69
  • 7
  • The warning is because `mail` is of length 1 whereas `Datacheck$Data[-1]` is length `nrow(Datacheck) - 1`. You can remove that line from the loop because it does not impact anything. Although I am not sure why it sends email to only 1st value. From the code it looks like this should work correctly. Maybe something related to `outMail = outApp$CreateItem(0)` ? – Ronak Shah Jul 18 '21 at 08:10

1 Answers1

1

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45