2010-11-22 269 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

相關問題