Skip to content

Sending emails from R under Windows

3 messages · David Khabie-Zeitoune, Jason Turner, David James

#
Hi

Does anyone know of any R routines to send emails from R, under Windows?
I thought about writing such a facility using the R(D)COM package to
drive e.g. MS Outlook, but I don't want to reinvent the wheel. I have
found a function Sys.mail in the library syskern, but this only works
under Unix by shelling out a mail command. 

Thanks,

David
#
David Khabie-Zeitoune wrote:
I've used R to call a perl script to handle the mailing; both the raw 
mail commands, plus the compression of attachments, mime encoding of 
plots or data files, etc.  Haven't got it handy, but (being perl) it 
should be very platform independant doing things that way.

You could run the script either as a system() command, or possibly using 
the RSperl library from Omegahat (haven't used the latter - just 
speculating).

Check out the MIME::Parser and Mail::Sender perl modules.

Cheers

Jason
#
David Khabie-Zeitoune wrote:
The R function sendEmail() below uses the RDCOMClient package
to control Outlook or Echange -- more precisely, it interfaces to
Microsoft's Collaboration Data Objects (CDO).  (Please note that 
I don't use Windows for email, so I couldn't test the function.)
name = "R-help mailing list",
            subject = "How to send Email from R using the RDCOMClient"
            msgBody = "here is the body of the message")

The package RDCOMClient is available at http://www.omegahat.org/RDCOMClient.

"sendEmail" <-
function(ema, name, subject, msgBody, deliverNow = TRUE)
{
   require(RDCOMClient)

   ema <- paste("SMPT:", ema, sep="")   ## prepend protocol to address

   ## create an e-mail session 
   session <- COMCreate("Mapi.Session") 
   session$Logon()

   ## add a message to the outbox collection of messages
   outbox <- session[["Outbox"]]
   msg <- outbox[["Messages"]]$Add(subject, msgBody)

   ## add recipient's name  (TODO: addMultiple() or loop, if many recipients)
   msg[["Recipients"]]$Add(name, ema) 
   msg$Send()
   if(deliverNow)
      msg$DeliverNow()

   session$Logoff()   ## wrap up
}