Sending Email From R

October 3, 2014

(This article was first published on Bearded Analytics » R, and kindly contributed to R-bloggers)

When I am am working in Sql Server and need to send an email I use “sp_send_dbmail“. So when I am working in R, I didn’t know how to send an email. I often use this as notification that a process has finished. It also works nicely as a text to your cell phone. I had one additional reason why I wanted to be able to email from R. I wanted to send an email to my evernote account with just a few key strokes. The goal was to accomplish this by writing a simple wrapper function. Below is the solution that I came up with. It works, but there are serious security implications. I offer this merely as a proof of concept. Hopefully someone can show me a better way to handle passing your email password to the R function.

# install.packages("mailR")
en <- function(s="Message From R",tags="#todo",b=" ",r = c("[email protected]")){
    sender <- "[email protected]"
    password <- readLines("pathtopassword/password.txt")
    if(r=="me") r <- "[email protected]"
        from = sender,
        to = r,
        body =b,
        smtp = list( = "", port = 465, = sender, passwd = password, ssl = TRUE),
        authenticate = TRUE,
        send = TRUE

I often have an R terminal open, so when I have a great idea for a research project I can add a note to my evernote account simply. For instance:

en(s="Read up on current imputation methods",tags="#Research")

Then a new entry is added for me in evernote with the tag ‘Research’. ( I have noticed that the tagging seems to only work if the tag previously exists in my evernote account.)  Often I have a task that just needs done that I don’t want to forget about. I can issue a quick command and then I will have a record of it.

en(s="email adviser about research")

That is all I need to do and the note is added to my account. I have found this to be quite useful and hopefully you will as well.

To leave a comment for the author, please follow the link and comment on their blog: Bearded Analytics » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)