当前位置:  开发笔记 > 编程语言 > 正文

如何使用R发送/接收(SMTP/POP3)电子邮件?

如何解决《如何使用R发送/接收(SMTP/POP3)电子邮件?》经验,为你挑选了2个好方法。

我强烈怀疑最受欢迎的答案是"这是工作的错误工具".我承认是R可能不是特别适合用于发送和接收电子邮件,但它是脚本语言,我知道最好的.我希望能找到一种方法,在R.发送和接收短邮件有谁知道一个既定的方式做到这一点在Windows平台上?我或许可以使用BLAT和GetMail的组合,但首选原生R解决方案.

编辑:可接受的解决方案应该能够与需要SSL的服务器连接.

编辑2:我提供了80%的回答.可悲的是,没有证明R本地方式.相反,我使用系统调用和命令行程序的邪恶组合,这些程序很可能不会跨平台兼容.R本地调用将需要深入研究POP3服务器与连接客户端交互的方式,以及对我目前没有的SSL的理解.其他答案仍然鼓励.

##Note: Other programs are wrapped in R functions and system calls.
#They each have their own licenses which may or may not allow the use suggested here
#Programs used here:
#STunnel: http://www.stunnel.org/; Provides an SSL tunnel but requires OpenSSL 
#OpenSSL: http://www.openssl.org/; OpenSSL to actually provide SSL
#   Note that these .dlls should be placed with the stunnel exe.
#   Also note that libssl32.dll may need to be renamed from ssleay32.dll
#Microsoft Visual C++ 2008 Redistributable (may be required for the SSL .dlls to work correctly)
#Blat: http://www.blat.net; a public domain SMTP sending program
#Getmail is free for non-commercial use. If you use it in a business environment, then a fee of $50 USD is payable to Tim Charron. 

#Stunnel is a TSR, so it will need to be killed from the task manager if there is an issue.  If you are willing to install it as a service you may be able to tweak my code to start and stop the service.  
#My current code does not create .conf file for stunnel the way a full version ought.  Check http://spampal.sanesecurity.com/manual_eng/servers/stunnel/stunnel.htm#sconfig21 to create the appropriate configuration file.

#Set the config values as appropriate
##Config##
BLAT.loc <- "c:/Programming/R/Rmail/blat262/full/blat.exe"
GetMail.loc <- "C:/Programming/R/RMail/getmail133/getmail.exe"
stunnel.loc <- "C:/Programming/R/RMail/stunnel/stunnel-4.11.exe"

#The set mail function assigns the username and password to be used as well as the smtp and pop3 servers it starts stunnel (and assumes that the stunnel.conf file is present and set correctly).
setMail <- function(user,pw,SSL=FALSE,smtp="127.0.0.1:259",pop3="127.0.0.1:1109")
{
    if (SSL==TRUE)
    {
        print("Starting stunnel; you will need to kill this from the task-manager")
        system(stunnel.loc,wait=FALSE)
        Sys.sleep(2) #Give it time to start 
    }
    return(list(user=user,pw=pw,smtp=smtp,pop3=pop3,SSL=SSL))
}

#function to send mail, myMail is the resulting list from setMail
sendmail <- function(myMail, to, subject, msg,VERBOSE=FALSE)
{
    writeLines(msg, "out.txt", sep = "\n", useBytes = FALSE)
      targ <- paste(getwd(),"/out.txt",sep="")
    call <- paste(BLAT.loc, ' "',targ,'" -subject "',subject,'" -to ',to," -u ",myMail$user," -pw ",myMail$pw, " -f ",myMail$user, " -debug -server ",myMail$smtp,sep="")
    res <- system(call,intern=TRUE)
    if (VERBOSE) {return(res)}
}

#function to get mail, myMail is the resulting list from setMail; it returns a list with one element that contains everything unparsed, another list provides the number of messages remaining on the server.
getmail <- function(myMail,VERBOSE=FALSE)
{      
    unlink("MSG1.txt") #drop previous get
    #download next message
    call <- paste(GetMail.loc," -u ",myMail$user," -pw ",myMail$pw," -s ",strsplit(myMail$pop3,":")[[1]][1],
        " -port ",strsplit(myMail$pop3,":")[[1]][2]," -n 1",sep="")
    res <- system(call,intern=TRUE)
    if (VERBOSE) {print(res)}
    nmsgtxt <- res[grep("messages on the server.",res)]
    nstart <- regexpr("There are",nmsgtxt)
    nend <- regexpr("messages on the server.",nmsgtxt)
    nmess <- as.numeric(substr(nmsgtxt,10,nend-1))-1
      x <- readLines("MSG1.txt",-1)
    return(list(message=x,remaining=nmess))
}

使用情况:简单地说,我需要具有R能够发送消息,其内容在R脚本别处确定到SMTP服务器.参与者将收到电子邮件并回复.我需要从我的POP3服务器检索他们的响应并将其存储在R数据结构中,以便我可以对其执行后期处理.在实践中,我建立的方式通过R.也就是说,R可以通过电子邮件将参与者做经验取样"你今天(1 =差; 7 =大)?" 参与者可以回答"4",我可以在数据库中匹配问题,响应等,以进行统计分析.



1> JD Long..:

从Pop服务器中提取消息

为了实现@JorisMeys利用其他语言的想法,我尝试使用Python和rJython包从Gmail(通过ssl)中提取邮件.Jython是在Java虚拟机上实现的Python,所以使用rJython对我来说有点像使用R来调用Java然后假装是Python.

我觉得rJython对于简单的事情很容易,但由于我不熟悉S4对象和(r)Java,我有时很难从rJython中正确地操作返回对象.但是,它有效.这是一个从Gmail帐户中提取单个邮件的基本结构:

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



2> Dirk Eddelbu..:

查看CRAN 上的sendmailR包.


这不在我的技能范围内.我知道自己的局限性,并且正在寻找可以在其中运作的解决方案.我不是一个贸易程序员,我是必需的.
sendmailR似乎只发送邮件; 它也不支持SSL.
推荐阅读
jerry613
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有