【发布时间】:2015-03-25 05:05:02
【问题描述】:
我强烈怀疑最受好评的答案将是“那是工作的错误工具”。我承认 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”,我可以在数据库中匹配提出的问题、回答等进行统计分析。
【问题讨论】:
-
类似(但不是 SSL 的重复原因)stackoverflow.com/questions/2885660/… 和 stackoverflow.com/questions/3572607/…
-
Edit2 和 Edit3 的基调是怎么回事?这真的需要吗?
-
在旁注中,Perl(以及 Python 的扩展)仍然是许多网站用作后端的主要脚本语言。它包含大量用于(自动)发送和接收电子邮件的功能(许多邮件列表利用 Perl 中的这些功能)。我也是一名程序员,但我很高兴我学会了使用多个“工具”(即语言)。从长远来看,这似乎是一个更好的方法。另外,Perl 和 R 可以链接。当然,或者 Python,它也可以做到这一点。
-
我了解到您需要一个跨平台的工具。您应该使用一些 *NIX 应用程序安顿下来,并尝试在 Windows 中运行它。
mutt是一个不错的选择,你可以通过 CygWin 运行它...这不是最优雅的解决方案,但目前sendmailR不支持 SSL。 -
作为一名非专业人士,我为我对“接收”一词的“草率”使用表示歉意。通过接收我的意思是从邮件服务器传输一封电子邮件,例如POP3 服务器,连接到本地计算机。在 R 中“可行”的东西,但似乎没有当前的库或正在开发的库可以做到这一点(这是我在重新发明轮子之前想要确定的一部分)。