[R] Does R have a command for sending emails?

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Tue May 10 11:01:31 CEST 2005


On 10-May-05 Fernando Saldanha wrote:
> Is there a way to have an R program send an email?
> 
> Something like this:
> 
> address <- 'abc at d.com'
> text <- 'This is the email body'
> send.email(address, text)

As Brian Ripley said, this is highly OS-specific!

On the other hand, studying 'bug.report' may be excessive.

*Provided* you are using a Unixoid system (Unix, Linux,
and as far as I know Mac OS X), you will almost certainly
have the 'mail' command which can be readily used for a
simple email such as the one you describe above.

At the system level, a possible command could be

  mail -s "subject of mail" abc at d.com << EOT
  "This is the email body"
  EOT

so all you need is an R function which binds all these
elements together.

For example, the following works on my Linux system:

  send.mail<-function(addr,subject="Mail from R",
                    text="empty text"){
    mail.cmd<-paste("mail ",
                    "-s \"",subject,"\" ",
                    addr,
                    " << EOT &\n",
                    text,"\n",
                    "EOT",
                    sep="",collapse="")
     system(mail.cmd,intern=FALSE)
  }

For example, with:

  send.mail("ted at localhost",
  subject="Message from R: At Last!",
  text="Good Morning!\nI've been busy all night.\nIt's finished."
  )

after a minute or two for delivery I get the following message
delivered on my machine:

  From ted at compo.my.LAN  Tue May 10 09:50:27 2005
  Date: Tue, 10 May 2005 09:49:50 +0100
  From: Ted Harding <ted at compo.my.LAN>
  To: ted at compo.my.LAN
  Subject: Message from R: At Last!

  Good Morning!
  I've been busy all night.
  It's finished.

I've specified "intern=FALSE" explicitly (though it is the
default), since if you set it TRUE (e.g. in order for R to
verify the sending) then R will hang until it receives the
feedback from the command (the "&" in "EOT &\n" puts the job
in the background so there is no delay).

Hoping this helps! (Of course, if you're not a unixoid, this
is probably no use to you at all, and I have no idea how to
do anything similar in Windows).

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 10-May-05                                       Time: 10:01:21
------------------------------ XFMail ------------------------------




More information about the R-help mailing list