2010-11-22 274 views
10

我强烈怀疑最有回报的答案是“这是工作的错误工具”。我承认R可能不太适合发送和接收电子邮件,但它是我最熟悉的脚本语言。我希望能找到一种在R中发送和接收短电子邮件的方法。有没有人知道在Windows平台上完成此操作的既定方法?我可能会使用BLAT和GetMail的组合,但本机R解决方案将是首选。如何使用R发送/接收(SMTP/POP3)电子邮件?

编辑:一个可接受的解决方案应该能够与需要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

类似(但不重复的原因的SSL)http://stackoverflow.com/questions/2885660/how-to-send-email-with-attachment-from-r-in-windows和http://stackoverflow.com/questions/3572607/sendmailr-part2 - 发送文件作为邮件附件 – Marek 2010-11-22 08:56:29

+1

Edit2和Edit3的语调如何?这是真的吗? – 2010-11-22 13:41:31

+2

在旁注中,Perl(以及Python扩展)仍然是许多网站用作后端的主要脚本语言。它包含丰富的(自动)发送和接收电子邮件功能(许多邮件列表利用Perl中的这些功能)。我也是一名程序员,但我很高兴我学会了使用多种“工具”(即语言)。从长远来看,这似乎是一个更好的方式。另外,Perl和R可以链接。或者Python,当然也可以。 – 2010-11-22 13:46:44

回答

11

从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(\'[email protected]\')") 
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

谢谢你指点我这个方向。我很快就会尝试一下,看看我是否可以使用它来发送邮件。我更喜欢这种方法来处理我在放在一起的命令行黑客。 – russellpierce 2010-11-23 01:50:22

+0

这真的是一个很好的开始。有没有办法提高回应? – IndranilGayen 2016-10-23 13:05:35

7

看看CRAN上的sendmailR包。

+2

sendmailR似乎只发送邮件;它也不支持SSL。 – russellpierce 2010-11-22 03:31:12

+3

所以写一个补丁吧。 – 2010-11-22 03:53:16

+10

这不属于我的技能范围。我知道我的局限性,并且正在寻找可以在他们内部工作的解决方案。我不是贸易程序员,我是一个必然的人。 – russellpierce 2010-11-22 08:18:31

1

随着mailR包(http://rpremraj.github.io/mailR/),你可以发送电子邮件与SSL:

send.mail(from = "[email protected]", 
      to = c("[email protected]", "[email protected]"), 
      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

尽管我对此的直接需求已经过去,但我仍然在Windows 7 x64系统上使用mailR软件包。它按照广告方式进行,没有任何摆弄我的工作。我想知道我们是否会很快在CRAN上看到它。 – russellpierce 2014-05-27 19:06:06

+0

...实现接收电子邮件的方式的任何计划? – russellpierce 2014-05-27 19:10:51

+1

mailR已经在CRAN上(http://cran.r-project.org/web/packages/mailR/index.html)。我将考虑增加对将来收到电子邮件的支持...... – 2014-05-27 19:53:28

相关问题