4

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!

Methexis
  • 2,739
  • 5
  • 24
  • 34

3 Answers3

7

In the source code for sendmail it states...

if (length(to) != 1) 
        stop("'to' must be a single address.")

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 ) ) 
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
1

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)
Rahul Premraj
  • 1,595
  • 14
  • 13
0

This does the trick for me: 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))
OB83
  • 476
  • 2
  • 10