Sendmailr (Part2): Sending Files as Mail Attachments

sendmailR (Part2): Sending files as mail attachments

With the mailR package (https://github.com/rpremraj/mailR), you could send HTML emails and additionally attach files with ease as below:

send.mail(from = "sender@gmail.com",
to = c("recipient1@gmail.com", "recipient2@gmail.com"),
subject = "Subject of the email",
body = "<html>The apache logo - <img src=\"http://www.apache.org/images/asf_logo_wide.gif\"></html>",
html = TRUE,
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
attach.files = c("./download.log", "upload.log"),
authenticate = TRUE,
send = TRUE)

Edit (2014-05-13):

mailR has been updated to allow different character encodings. Below is an example to send the message as UTF-8.

send.mail(from = "Sender Name <sender@gmail.com>",
to = "recipient@gmail.com",
subject = "A quote from Gandhi",
body = "In Hindi : थोडा सा अभ्यास बहुत सारे उपदेशों से बेहतर है।
English translation: An ounce of practice is worth more than tons of preaching.",
encoding = "utf-8",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = T),
authenticate = TRUE,
send = TRUE)

sharing 2 data tables through sendmailR

Adding <p><pre>', paste(pander_return(pander(my_data2, style="multiline", caption = "New Caption")), collapse = '\n'), '</pre> between </pre> and </body> should give you the desired result.

When you want to insert some text between the two tables, you can do:

<p>the text you want to insert between the tables</p>
<pre>', paste(pander_return(pander(my_data2, style="multiline", caption = "New Caption")), collapse = '\n'), '</pre>

sendmailR: Submit encoded message to local SMTP server

Mail client won't be able to understand any encoding without Content-Type: charset=..., so you must add it:

msg<-iconv("text in greek", to = "utf8");
sendmail(from, to, subject, msg,
control=list(smtpServer="blabla"),
headers=list("Content-Type"="text/plain; charset=UTF-8; format=flowed")
);

that is for UTF8 (which I believe should be used), for CP1253:

msg<-iconv("text in greek", to = "CP1253");
sendmail(from, to, subject, msg,
control=list(smtpServer="blabla"),
headers=list("Content-Type"="text/plain; charset=CP1253; format=flowed")
);

multisend by hidden copies can also be done with header magick, still I think sapply loop is a better idea -- then the user will see that the mail was send directly to her/himself.

How can I send/receive (SMTP/POP3) email using R?

Pulling messages from a Pop server

To take a stab at implementing @JorisMeys idea of taking advantage of other languages, I took a stab at pulling mail from Gmail (over ssl) using Python and the rJython package. Jython is Python implemented on the Java virtual machine, so using rJython feels to me a bit like using R to call Java that then pretends to be Python.

I find rJython pretty easy for simple things, but since I'm not well versed in S4 objects and (r)Java I sometimes struggle to properly manipulate the return objects from rJython. But, it works. Here's a basic construct that will pull a single message from a Gmail account:

library(rJython)

rJython <- rJython( modules = "poplib")

rJython$exec("import poplib")
rJython$exec("M = poplib.POP3_SSL('pop.gmail.com', 995)")
rJython$exec("M.user(\'yourGmailAddy@gmail.com\')")
rJython$exec("M.pass_(\'yourGmailPassword\')")
rJython$exec("numMessages = len(M.list()[1])")
numMessages <- rJython$get("numMessages")$getValue()

# grab message number one. Loop here if you
# want more messages
rJython$exec("msg = M.retr(1)[1]")
emailContent <- rJython$get("msg")

# turn the message into a list
contentList <- as.list(emailContent)
# so we have an R list... of Java objects
# To get a more native R list we have to
# yank the string from each Java item

messageToList <- function(contentList){
outList <- list()
for (i in 1:length(contentList)){
outList[i] <- contentList[[i]]$toString()
}
outList
}

messageAsList <- messageToList(contentList)
messageAsList


Related Topics



Leave a reply



Submit