2010-11-22 24 views
10

मुझे दृढ़ता से संदेह है कि सबसे अधिक उत्तर दिया गया जवाब "यह नौकरी के लिए गलत उपकरण है"। मैं स्वीकार करता हूं कि आर ईमेल भेजने और प्राप्त करने के लिए विशेष रूप से उपयुक्त नहीं हो सकता है, लेकिन यह स्क्रिप्टिंग भाषा है जिसे मैं सबसे अच्छी तरह जानता हूं। मैं आर में छोटे ईमेल भेजने और प्राप्त करने का एक तरीका ढूंढने की उम्मीद कर रहा हूं। क्या किसी को विंडोज प्लेटफ़ॉर्म पर ऐसा करने का एक स्थापित तरीका पता है? मैं BLAT और GetMail के संयोजन का उपयोग करने में सक्षम हो सकता हूं लेकिन मूल आर समाधान को प्राथमिकता दी जाएगी।आर का उपयोग करके मैं कैसे भेज/प्राप्त कर सकता हूं (एसएमटीपी/पीओपी 3) ईमेल?

संपादित करें: एक स्वीकार्य समाधान एसएसएल की आवश्यकता वाले सर्वरों के साथ इंटरफेस करने में सक्षम होना चाहिए।

संपादित करें 2: मैं 80% उत्तर पर अपना स्टैब प्रदान करता हूं। अफसोस की बात है कि एक आर मूल तरीका प्रदर्शित नहीं किया गया है। इसके बजाय मैं सिस्टम कॉल और कमांड लाइन प्रोग्राम के अपवित्र संयोजन का उपयोग करता हूं जो प्लेटफॉर्म पर संगत नहीं होने की संभावना है। आर देशी कॉलों को पीओपी 3 सर्वरों से जुड़े क्लाइंट्स और एसएसएल की समझ के तरीके से खोदने की आवश्यकता होगी जो मेरे पास वर्तमान में नहीं है। अन्य उत्तरों को अभी भी प्रोत्साहित किया जाता है।

##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)) 
} 

प्रयोग मामले: सीधे शब्दों में कहें, मैं आर संदेश जिनकी सामग्री SMTP सर्वर को आर लिपि में कहीं और निर्धारित किया जाता है भेजने में सक्षम होना की आवश्यकता है। प्रतिभागियों को ईमेल प्राप्त होगा और इसका जवाब मिलेगा। मुझे अपने पीओपी 3 सर्वर से अपनी प्रतिक्रिया पुनर्प्राप्त करने और इसे आर डेटा संरचना में स्टोर करने की आवश्यकता है ताकि मैं उस पर पोस्ट प्रोसेसिंग कर सकूं। प्रैक्टिस में, मैं आर के माध्यम से अनुभव नमूना करने का एक तरीका स्थापित कर रहा हूं। यही है, आर एक प्रतिभागी को ईमेल कर सकता है "आज आप कैसे हैं (1 = खराब; 7 = ग्रेट)?" प्रतिभागी "4" का जवाब दे सकता है, और मैं सांख्यिकीय विश्लेषण के लिए डेटाबेस में पूछे गए प्रश्न, प्रतिक्रिया, और इसी तरह से मेल खाता हूं।

+1

इसी प्रकार के (लेकिन एक नकली कारण एसएसएल का) http://stackoverflow.com/questions/2885660/how-to-send-email-with-attachment-from-r-in- विन्डोज़ और http://stackoverflow.com/questions/3572607/sendmailr-part2 -sending-files-as-mail-attachments – Marek

+1

संपादन 2 और संपादन 3 के स्वर के साथ क्या हो रहा है? क्या वास्तव में इसके लिए बुलाया जाता है? –

+2

एक साइडेनोट पर, पर्ल (और एक्सटेंशन पायथन द्वारा) अभी भी एक मुख्य स्क्रिप्टिंग भाषा है जो कई वेबसाइटों के लिए बैकएंड के रूप में उपयोग की जाती है। इसमें ई-मेल भेजने और प्राप्त करने के लिए (स्वचालित) के लिए कार्यों का एक धन शामिल है (कई मेलिंग सूचियां पर्ल में इन कार्यक्षमताओं का लाभ लेती हैं)। मैं आवश्यकता के साथ एक प्रोग्रामर भी हूं, लेकिन मुझे खुशी है कि मैंने एक से अधिक "टूल" (यानी भाषा) के साथ काम करना सीखा। लंबे समय तक, यह जाने का एक बेहतर तरीका लगता है। इसके अलावा, पर्ल और आर को जोड़ा जा सकता है। या पायथन बंद कोर्स, यह भी कर सकते हैं। –

उत्तर

11

एक पॉप सर्वर

से संदेश पुलिंग अन्य भाषाओं का लाभ लेने के @JorisMeys विचार को लागू करने पर एक वार लेने के लिए, मैं (SSL पर) जीमेल से मेल खींच अजगर और rJython का उपयोग करने में एक चाकू ले लिया पैकेज। जेथॉन जावा वर्चुअल मशीन पर पाइथन लागू किया गया है, इसलिए आरजेथन का उपयोग करके मुझे जावा को कॉल करने के लिए आर का उपयोग करने की तरह थोड़ा लगता है जो तब पाइथन होने का नाटक करता है।

मुझे लगता है कि साधारण चीजों के लिए आरजेथन बहुत आसान है, लेकिन चूंकि मैं एस 4 ऑब्जेक्ट्स में अच्छी तरह से नहीं जानता हूं और (आर) जावा मैं कभी-कभी आरजेथन से रिटर्न ऑब्जेक्ट्स में सही तरीके से हेरफेर करने के लिए संघर्ष करता हूं। लेकिन, यह काम करता है। यहाँ एक बुनियादी निर्माण कि एक जीमेल खाते से किसी एक संदेश खींच लेंगे है: mailR पैकेज (http://rpremraj.github.io/mailR/) के साथ

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

+0

यह वास्तव में बहुत अच्छी शुरुआत है। क्या प्रतिक्रिया को बढ़ाने का कोई तरीका है? – IndranilGayen

7

CRAN पर sendmailR पैकेज देखें।

+2

sendmailR केवल मेल भेजता है; यह एसएसएल का समर्थन नहीं करता है। – russellpierce

+3

तो इसके लिए एक पैच के लिए लिखें। –

+10

यह मेरे कौशल सेट के भीतर नहीं है। मैं अपनी सीमाओं को जानता हूं और एक समाधान की तलाश में था जो उनके भीतर काम करेगा। मैं व्यापार द्वारा प्रोग्रामर नहीं हूं, मैं आवश्यकता से एक हूं। – russellpierce

1

, आप एसएसएल के साथ ईमेल भेज सकते हैं:

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

हालांकि इसके लिए मेरी तत्काल आवश्यकता पिछले है, मैंने मेलआर पैकेज को विंडोज 7 x64 सिस्टम पर एक घुमाव दिया है। यह काम करने के लिए मेरे हिस्से पर किसी भी झुकाव के बिना विज्ञापन के रूप में प्रदर्शन किया। मुझे आश्चर्य है कि हम इसे जल्द ही क्रैन पर देख पाएंगे। – russellpierce

+0

... ईमेल प्राप्त करने के तरीके को लागू करने की कोई योजना? – russellpierce

+1

मेलआर पहले ही सीआरएएन (http://cran.r-project.org/web/packages/mailR/index.html) पर है। मैं भविष्य में ईमेल प्राप्त करने के लिए समर्थन जोड़ने पर विचार करूंगा ... –

संबंधित मुद्दे

 संबंधित मुद्दे