Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have successfully managed to implement the sendmailR function to send one message to one recipient.

Do you know if it is possible to send that same message to multiple recipients within the function? A form of CC'ing?

If not I think the only way is to loop round on a variable, which would normally be okay but for my current code would result with a loop within a loop and make things fairly and hopefully unnecessary complex

I cant see anything in the documentation that would appear to indicate functionality like this --> http://cran.r-project.org/web/packages/sendmailR/sendmailR.pdf

Thanks for any help, I will keep testing to see if there is a work around inm the meantime!

So this leaves you with several options (all of which are loops).The execution time of a loop compared to sending the email will be negligible. A couple of options are:

Option 1

Use Vectorize to vectorise the to argument of sendmail , allowing you to supply a character vector of email addresses to send to...

sendmailV <- Vectorize( sendmail , vectorize.args = "to" )
emails <- c( "me@thisis.me.co.uk" , "you@whereami.org" )
sendmailV( from = "me@me.org" , to = emails )

Option 2

Using sapply to iterate over the a character vector of email addresses applying the sendmail function each time...

sapply( emails , function(x) sendmail( to = "me@me.org" , to = x ) ) 
                @Rahul That was not an option at the time the question was answered. Perhaps you should add that as a new answer, now that this package is available.
– Simon O'Hanlon
                May 10, 2014 at 14:55

You could try the development version of the mailR package available on github https://github.com/rpremraj/mailR

Using mailR, you could send an email in HTML format as below:

send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          cc = c("CCrecipient1@gmail.com", "CCrecipient2@gmail.com"),
          subject = "Subject of the email",
          body = "<html>The apache logo - <img src=\"http://www.apache.org/images/asf_logo_wide.gif\"></html>",
          html = TRUE,
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)
Define from, msg, subject, body seperatly:

from <- sprintf("<sendmailR@%s>", Sys.info()[4]) 
.....
TO <- c("<adres1@domain.com>", "<adres2@domain.com>")
sapply(TO, function(x) sendmail(from, to = x, subject, msg, body))
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.