Skip to content

Does R have a command for sending emails?

9 messages · Brian Ripley, Philippe GROSJEAN, (Ted Harding) +4 more

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

Thanks.

FS
#
On Mon, 9 May 2005, Fernando Saldanha wrote:

            
No.  There have been proposals to do this, but it is highly OS-specific, 
and also likely to hit security issues.

You could take a look at what bug.report() does (which is nothing on some 
platforms).

  
    
#
Prof Brian Ripley wrote:
...but on most, if not all systems, you can use command-line 
applications to send emails... and since R can start command-line 
applications with system(), you should get that functionnality easily 
"incorporated" into R. Just use Google a little bit!
Best,

Philippe Grosjean
#
On 10-May-05 Fernando Saldanha wrote:
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 ------------------------------
#
How about an R package to talk to google's gmail service, perhaps 
leveraging an existing API, such as:

http://libgmail.sourceforge.net/

  this is written in pure python, so not only is it cross-platform but 
it should be possible to write one in pure R using HTTP calls and the like.

  you could then do statistical analysis of your gmail spam purely in R.

  or am I now getting too silly?

Baz
#
On 5/10/05, Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> wrote:
Not yet.

However, one could just connect via sockets to port 25 (smtp port) and
do an appropriate mail sending dialog.

Not too hard, provided that you get the details right and handle any
security issues.
#
On Tue, 10 May 2005, Philippe Grosjean wrote:

            
Yes, that is what bug.report does.  I don't know of one that is guarenteed 
to exist on Windows: the `blat' that Uwe Ligges mentioned is not standard.
(Windows does have a standard set of system calls, and they are quite 
likely to be firewalled from unknown applications like R.)
#
Fernando Saldanha wrote:
Under Linux/Unix you can use code such as the following.  This handles 
kmail and mail.

     if(mailer=='kmail') {
       tf <- tempfile()
       cat(cmd, file=tf)
       to <- paste('"', paste(to, collapse=','), '"', sep='')
       if(length(cc)) cc <- paste(' -c "', paste(cc, 
collapse=','),'"',sep='')
       if(length(bcc)) bcc <- paste(' -b "', paste(bcc, 
collapse=','),'"',sep='')
     } else {
       to <- paste(to, collapse=' ')
       if(length(cc))  cc  <- paste(paste(' -c', cc), collapse='')
       if(length(bcc)) bcc <- paste(paste(' -b', bcc),collapse='')
     }
     cmd <- if(mailer=='kmail') paste('kmail -s "', title, '"', cc,
                 bcc, ' --msg ', tf, ' ', to, sep='') else
       paste('echo -e "', cmd, '" | mail -s "',
             title, ' Reports"', cc, bcc, ' ', to, sep='')
     system(cmd)
#
I want to thank all who have offered help on this topic. I was able to
create a very simple email function that I have tested to work under
Windows XP Professional and R 2.1.0. It uses Blat version 1.9.4.

send.mail<-function(addr, subject, source.file) {
  mail.cmd <- paste("Blat", source.file, "-subject", dQuote(subject),
"-to", addr, separator = " ", collapse = "")
  
  system(mail.cmd, intern = FALSE)
}

The string source.file must have double backslashes instead of single
backslashes. For example:

C:\\myfolder

One must first install Blat version 1.9.4, available at 

http://www.blat.net/194/.

All that is needed is to unzip the downloaded file (Blat194.zip) and
copy Blat.exe to a folder in the path. The other files inside
Blat194.zip can be discarded.

FS
On 5/10/05, Frank E Harrell Jr <f.harrell at vanderbilt.edu> wrote: