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:> Is there a way to have an R program send an email?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).> Something like this: > > address <- 'abc at d.com' > text <- 'This is the email body' > send.email(address, text)-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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 ------------------------------
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) > > Thanks. > > FS > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >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) -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
OlsenN@pac.dfo-mpo.gc.ca
2005-May-10 15:39 UTC
[R] Does R have a command for sending emails?
At the risk of beating this to death ... if you use Outlook mail on Windows,
you can create a simple 'sendmail' vbscript:
' ==== start ==Dim pOutlook, pMail, fso, f
Set pOutlook = CreateObject("Outlook.Application")
Set pMail = pOutlook.CreateItem(olMailItem)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(WScript.Arguments(2), 1)
With pMail
.To = WScript.Arguments(0)
.Subject = WScript.Arguments(1)
.Body = f.ReadAll()
.Send
End With
f.Close()
' ==== end ===
And then call this from R:
send.mail <- function(addr, subject, source.file) {
mail.cmd <-
paste(paste(Sys.getenv("SystemRoot"),"/system32/wscript.exe",sep=""),
"sendmail.vbs", addr, dQuote(subject), source.file, sep="
")
system(mail.cmd, intern=F)
}
Note that if sendmail.vbs is not in the current R working directory you have
to provide a full path.
Norm Olsen
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Fernando Saldanha
Sent: Tuesday, May 10, 2005 6:28 AM
To: Submissions to R help
Subject: Re: [R] Does R have a command for sending emails?
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:> 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)
> >
> > Thanks.
> >
> > FS
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
>
> 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)
>
> --
> Frank E Harrell Jr Professor and Chair School of Medicine
> Department of Biostatistics Vanderbilt University
>
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
This is excellent. It works great even though I'm on Win2k and behind a
firewall.
I just have one question though, I don't use a function because I sometimes
need to add people to my distribtion list. In R, my system(paste()) string
is very long and when I try put an <enter> so it fits in my Rgui screen,
the
function does not work properly. Everything that is after the <enter> is
not considered by blat.
I don't know if I am being clear. To clarify, here's my actual code:
system(paste('
blat c:/tmp/monthly/deacot2005.zip -to receiver@receiver.ca -f
sender@sender.ca -s "My title is long long long long. Very long."
-server
smtp.host.com -base64'))
In R, if everything beginning with blat is not on the same line, the command
does not work properly and everthing on the second line is not considered
even thought he paste command ends with a ")"
Thanks
If you're still trying to figure out how to e-mail with R, here's what I
did
in details.
1- Download blat from
http://sourceforge.net/project/showfiles.php?group_id=81910
<http://sourceforge.net/project/showfiles.php?group_id=81910>
2- unzip blat.exe in your base directory (the one with Rgui.exe in it). I
put it in my C:\Program Files\R\rw2010\bin directory. You do not need the
other files in the zip file.
3- if you don't know your smtp, use this
http://www.dirfile.com/smtp_diagnostic_tool.htm
<http://www.dirfile.com/smtp_diagnostic_tool.htm> . Put in your e-mail
address and leave blank the SMTP. Press start.
4- In R,
Write this code and change what you want
system(paste('
blat c:/tmp/monthly/deacot2005.zip -to receiver@receiver.ca -f
sender@sender.ca -s "My title is long long long long. Very long."
-server
smtp.host.com -base64')) #everything beginning with "blat" has to
be on the
same line
5- read the example.txt in the blast zip file for examples of how to modify
the blat request
6- note: the "-base64" statement is there to specify that the
attached file
is binary
***********************************************************************************
AVIS DE NON-RESPONSABILITE:
Ce document transmis par courrier electronique est destine uniquement a la
personne ou a l'entite a qui il est adresse et peut contenir des
renseignements confidentiels et assujettis au secret professionnel. La
confidentialite et le secret professionnel demeurent malgre l'envoi de ce
document a la mauvaise adresse electronique. Si vous n'etes pas le
destinataire vise ou la personne chargee de remettre ce document a son
destinataire, veuillez nous en informer sans delai et detruire ce document ainsi
que toute copie qui en aurait ete faite.Toute distribution, reproduction ou
autre utilisation de ce document est
strictement interdite. De plus, le Groupe Financiere Banque Nationale et ses
filiales ne peuvent pas etre tenus responsables des dommages pouvant etre causes
par des virus ou des erreurs de transmission.
DISCLAIMER:\ This documentation transmitted by electronic ma...{{dropped}}
> 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) >Others have shown you how you can wrap external programs with R for sending mail on unixoid systems. On windows you can install Cygwin and have the same functionality. I particulary like mutt, since one can also send attachments. Something like this can be very usefull: mutt -x -s subject -a attachment email at hfjdhsj -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana Biotechnical Faculty URI: http://www.bfro.uni-lj.si/MR/ggorjan Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si Groblje 3 tel: +386 (0)1 72 17 861 SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia, Europe ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C.