Skip to content

Instantly share code, notes, and snippets.

@bedantaguru
Created October 23, 2016 13:34
Show Gist options
  • Save bedantaguru/0494fba5af6841936a07f2cd77b90393 to your computer and use it in GitHub Desktop.
Save bedantaguru/0494fba5af6841936a07f2cd77b90393 to your computer and use it in GitHub Desktop.
Get Mails from Gmail in R
# credit goes to http://stackoverflow.com/questions/4241812/how-can-i-send-receive-smtp-pop3-email-using-r
library(rJython)
rJython <- rJython( modules = "poplib")
rJython$exec("import poplib")
rJython$exec("M = poplib.POP3_SSL('pop.gmail.com', 995)")
rJython$exec("M.user(\'<username>@gmail.com\')")
rJython$exec("M.pass_(\'<password>\')")
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment