It seems that everything is possible with R. My latest discovery is that you can easily send emails from the R console. It can be very useful for sending reporting files directly to your address, or even to a mailing list. Some people – I shall not mention any names – might also think of the possibility to create documents that differ for each person of a mailing list (mid terms exams specifically…).

I am using this functionality these days for quite a different purpose, even if it is kind of related to reporting. Everyday, I collect data from a website, and I would like to know quickly if all went well, because the website is changing day after day. If something comes up, it is then easier to spot, and the time gained is significant

So, I found some useful pages on the Internet explaining how to send emails with R, using the sendmailR package. One of the relevant ones was on stackoverflow.

Now you might wonder why I wrote this note on my blog, if everything is already explained on this page. The reason is that I am a complete novice with the terminal, and I spent some time figuring out how to finally make the sendmailR package work on my computer. Hence, I would like to explain my approach with my own words.

The first thing you need to do is to configure (if it is not already) your machine so it can send emails from Localhost. To do so, follow this tutorial, step by step. I added an extra step before the third one. Maybe I did something wrong while following the instructions, but at the end of the day, I am left with a file names sasl_passwd.db on which my gmail password is not crypted… This is problematic. So, this is what I did : (i) remove the sasl_passwd file in order to keep only the sasl_passwd.db one ; and then (ii) change the reading/writing permissions of the sasl_passwd.db file, so that users can’t access it. To do so, this is the bash command I typed in the terminal, while being on the directory containing the sasl_passwd.db file:

\$ chmod u+rw-x,g-rwx,o-rwx sasl_passwd.db


Then, after completing the tutorial, my computer was able to send emails from the terminal, using Gmail’s smtp. Quite useful!

The final step is to write a few R lines:

library(sendmailR)
from <- sprintf("", Sys.info()[4])
to <- ""
subject <- paste("Report",format(Sys.time(),"%Y-%m-%d"),sep=" ")
body <- paste("Here is the ",format(Sys.time(),"%Y-%m-%d")," report",sep="")