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

后端 未结 3 1002
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 08:33

I strongly suspect the most upvoted answer will be \"that is the wrong tool for the job\". I acknowledge that R may not be particularly well suited for sending and receiving

相关标签:
3条回答
  • 2020-12-15 09:02

    Look at the sendmailR package on CRAN.

    0 讨论(0)
  • 2020-12-15 09:03

    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
    
    0 讨论(0)
  • 2020-12-15 09:15

    With the mailR package (http://rpremraj.github.io/mailR/), you could send emails with SSL:

    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)
    
    0 讨论(0)
提交回复
热议问题