José
José

Reputation: 931

How to send different contents to different recipients with mailR

I am trying to send different contents to different recipients, but all recipients get all contents. Any help?

library(mailR)
msg<- data.frame(recipients=c("[email protected]","[email protected]","[email protected]"),
                 messages=c("firstmsg","secondmsg","thirdmsg"))

for ( i in msg$recipients)  
{  
  for (j in msg$messages)  {
    send.mail(from="[email protected]",
              to= i,  
              body =  j,
              subject = "subject",
              encoding = "utf-8",  
              smtp= list(host.name = "smtp.gmail.com", port = 465, 
                         user.name = "[email protected]", passwd = "mypassword", ssl = TRUE),
              authenticate = TRUE,  send = TRUE,  attach.files=NULL,  debug = FALSE)
  }
}

Upvotes: 1

Views: 132

Answers (1)

Stedy
Stedy

Reputation: 7469

You are using a double loop which will go through each value of j for each value of i in your loop. One approach would be to use list indexing:

msg<-data.frame(recipients=c("[email protected]","[email protected]",
"[email protected]"),messages=c("firstmsg","secondmsg","thirdmsg"))

for i in 1:nrow(msg)  
{  
send.mail(from="[email protected]",
to= msg$recipients[i],  
body =  msg$message[i],
subject = "subject",
encoding = "utf-8",  
smtp= list(host.name = "smtp.gmail.com", port = 465, 
user.name = "[email protected]", passwd = "mypassword", ssl = TRUE),
authenticate = TRUE,  send = TRUE,  attach.files=NULL,  debug = FALSE)
}

Upvotes: 1

Related Questions