[R] Sending emails from R under Windows

David James dj at research.bell-labs.com
Wed Jul 30 16:13:18 CEST 2003

David Khabie-Zeitoune wrote:
> 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
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

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.)

> sendEmail(ema = "r-help at r-project.org", 
            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)

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

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

   ## 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) 

   session$Logoff()   ## wrap up

David A. James
Statistics Research, Room 2C-253            Phone:  (908) 582-3082       
Bell Labs, Lucent Technologies              Fax:    (908) 582-3340
Murray Hill, NJ 09794-0636

More information about the R-help mailing list