2010-05-21 29 views
47

Tôi có một tập lệnh R được lên lịch chạy từ máy tính Windows.Cách gửi email có tệp đính kèm từ R trong cửa sổ

Sau khi hoàn tất, tôi muốn tập lệnh này tự động gửi một email có đính kèm một số tệp nhật ký.

Sử dụng shell() với một số tập lệnh khác có thể có thể, nhưng tôi đã tự hỏi nếu có giải pháp tốt hơn trong R. Cảm ơn bạn.

+1

Tôi đoán bạn có nghĩa là 'system()' khi bạn viết 'shell()'. –

+0

Xin chào Ahala, tôi hy vọng giải pháp Stedy sẽ giúp bạn. Tôi có thể hỏi, bạn sử dụng những gì để có một kịch bản R theo lịch trình? Cảm ơn. –

+4

Tal, sử dụng tác vụ theo lịch trong Windows (tôi cho rằng mục nhập đó giống như R CMD BATCH script.R), cron trên * Nix sistems. – aL3xa

Trả lời

6

Bạn đã xem gói sendmailR chưa? Nó cho phép SMTP gửi một tin nhắn và bạn có thể chỉnh sửa chức năng để cho phép một phần đính kèm. Sau đó, một lần nữa, nếu nó chỉ có một tập tin đăng nhập nó chỉ có thể được giá trị nó để sử dụng shell() như bạn đã đề cập.

10

Bạn sẽ giải quyết một tin nhắn twitter? Bạn có thể sử dụng Rcurl để đăng cập nhật lên twitter, sau đó có thể chuyển tiếp tới điện thoại di động của bạn dưới dạng văn bản hoặc gửi email qua cài đặt thông báo.

Xem ở đây: http://www.sakana.fr/blog/2007/03/18/scripting-twitter-with-curl/

+4

Có một gói CRAN được gọi là twitteR để giao tiếp twitter từ R: http://cran.r-project.org/web/packages/twitteR/index.html –

+0

+1 để sử dụng Twitter, rất thông minh – Stedy

43

sendmailR làm việc cho tôi trên Windows 7. Tôi tham chiếu http://cran.es.r-project.org/web/packages/sendmailR/sendmailR.pdf

smtpServer = thông tin cho Outlook 2010 là trong File -> Account Settings -> Account Settings -> nhấp đúp chuột vào tài khoản của bạn -> văn bản trong "server" hộp

library(sendmailR) 

#set working directory 
setwd("C:/workingdirectorypath") 

#####send plain email 

from <- "[email protected]" 
to <- "[email protected]" 
subject <- "Email Subject" 
body <- "Email body."      
mailControl=list(smtpServer="serverinfo") 

sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl) 

#####send same email with attachment 

#needs full path if not in working directory 
attachmentPath <- "subfolder/log.txt" 

#same as attachmentPath if using working directory 
attachmentName <- "log.txt" 

#key part for attachments, put the body and the mime_part in a list for msg 
attachmentObject <- mime_part(x=attachmentPath,name=attachmentName) 
bodyWithAttachment <- list(body,attachmentObject) 

sendmail(from=from,to=to,subject=subject,msg=bodyWithAttachment,control=mailControl) 

Bên cạnh đó, nhiều tập tin có thể được gửi bằng cách thêm mime_part khác vào danh sách msg như sau (tôi cũng ngưng tụ nó):

attachmentObject <- mime_part(x="subfolder/log.txt",name="log.txt") 
attachmentObject2 <- mime_part(x="subfolder/log2.txt",name="log2.txt") 
bodyWithAttachment <- list(body,attachmentObject,attachmentObject2) 
+0

Bạn có thể sử dụng html trong "Nội dung email". Bạn có thể sử dụng mã hóa utf-8 (cho văn bản đa ngôn ngữ) không? –

+2

Tôi không biết. Hãy thử nó và xem. – ARobertson

+2

Làm cách nào để cấu hình phần serverinfo cho gmail? – qed

4

Đối với Windows, người dùng có thể phân tích cú pháp một VB-Script (xem ví dụ: http://www.paulsadowski.com/wsh/cdo.htm) và sau đó gọi nó qua vỏ.

này có thể trông như thế này:

SendMail <- function(from="[email protected]",to="[email protected]",text="Hallo",subject="Sag Hallo",smtp="smtp.my.server.de",user="me.myself.and.i",pw="123"){ 
require(stringr) 
part1 <- "Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 
Const cdoAnonymous = 0 'Do not authenticate 
Const cdoBasic = 1 'basic (clear-text) authentication 
Const cdoNTLM = 2 'NTLM " 

part2 <- paste(paste("Set objMessage = CreateObject(",'"',"CDO.Message",'"',")" ,sep=""), 
paste("objMessage.Subject = ",'"',subject,'"',sep=""), 
paste("objMessage.From = ",'"',from,'"',sep=""), 
paste("objMessage.To = ",'"',to,'"',sep=""), 
paste("objMessage.TextBody = ",'"',text,'"',sep=""), 
sep="\n") 

part3 <- paste(
"'==This section provides the configuration information for the remote SMTP server. 

objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/sendusing\") = 2 

'Name or IP of Remote SMTP Server 
objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/smtpserver\") = ",'"',smtp,'"'," 

'Type of authentication, NONE, Basic (Base64 encoded), NTLM 
objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate\") = cdoBasic 

'Your UserID on the SMTP server 
objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/sendusername\") = ",'"',user,'"'," 

'Your password on the SMTP server 
objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/sendpassword\") = ",'"',pw,'"', " 

'Server port (typically 25) 
objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/smtpserverport\") = 25 

'Use SSL for the connection (False or True) 
objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/smtpusessl\") = False 

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server) 
objMessage.Configuration.Fields.Item _ 
(\"http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout\") = 60 
objMessage.Configuration.Fields.Update 

'==End remote SMTP server configuration section== 

objMessage.Send 
",sep="") 

vbsscript <- paste(part1,part2,part3,sep="\n\n\n") 
str_split(vbsscript,"\n") 
writeLines(vbsscript, "sendmail.vbs") 
shell("sendmail.vbs") 
unlink("sendmail.vbs") 
} 
+2

Cảm ơn rất nhiều, công việc này giống như một giấc mơ! Đây là phương pháp duy nhất tôi đã tìm thấy để làm việc trong R (trên Windows). Như được giải thích trong trang web của Paul, nếu bạn sử dụng gmail, bạn cần đặt cổng máy chủ thành 465 và đặt tùy chọn "Sử dụng SSL" thành True. Tôi đã sử dụng đoạn mã sau để gọi nó: 'SendMail (from =" [email protected] ", to =" [email protected] ", text =" Hallo ", chủ đề =" Sag Hallo ", smtp =" smtp.gmail.com ", user =" [email protected] ", pw =" ******** ")' Chèn mật khẩu thực của bạn tất nhiên ... Ngoài ra, thư mục công việc của R phải ở đâu đó trên đĩa cứng (C :). –

+0

Điều này thật tuyệt vời! Có thể thêm tệp đính kèm không? Tôi có kinh nghiệm VB rất hạn chế ... vì vậy, đó là lý do tại sao tôi hỏi. – maloneypatr

+0

Yêu thích trợ giúp :-) ... jip, có vẻ như có thể mặc dù tôi chưa bao giờ thử bản thân mình, xem tại đây: http://www.paulsadowski.com/wsh/cdo.htm dưới dòng tiêu đề "Gửi email văn bản kèm theo tệp "... có thể bao gồm các dòng có liên quan trong hàm. – petermeissner

2

Chỉ muốn nhắc nhở mọi người ai muốn tính năng tự thông báo của một dịch vụ gọi twilio, họ cung cấp dịch vụ miễn phí để gửi sms đến điện thoại di động của riêng bạn. Đi bộ qua R bằng cách sử dụng tại đây https://dreamtolearn.com/ryan/data_analytics_viz/78

Mã ví dụ được đính kèm, chỉ cần thay thế bằng chứng xác thực bằng mã của riêng bạn.

library(jsonlite) 
library(XML) 
library(httr) 
library(rjson) 
library(RCurl) 
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))) 

authenticate_twilio <- "https://[ACCOUNT SID]:[AUTH TOKEN]@api.twilio.com/2010-04-01/Accounts" 
authenticate_response <- getURL(authenticate_twilio) 
print(authenticate_response) 

postForm("https://[ACCOUNT SID]:[AUTH TOKEN]@api.twilio.com/2010-04-01/Accounts/[ACCOUNT SID]/Messages.XML",.params = c(From = "+1[twilio phone#]", To = "+1[self phone#]",Body = "Hello from twilio")) 
12

- nó hoạt động với xác thực, tệp đính kèm, nó tự động gửi tin nhắn txt cùng với html và hơn thế nữa.

mailR yêu cầu rJava đôi khi có thể hơi đau. Trên các cửa sổ, tôi không gặp bất kỳ vấn đề gì. Trên ubuntu này đã giải quyết được một vấn đề tôi đã có:

sudo apt-get install openjdk-jdk 

trong R

install.packages("devtools", dep = T) 
library(devtools) 
install_github("rpremraj/mailR") 

(nếu bạn gặp khó khăn với rJava - thử sudo R CMD javareconf trong terminal)

mailR rất dễ dàng để làm việc với và cũng tài liệu trên trang github.

Ví dụ từ documentaion

library(mailR) 
send.mail(from = "[email protected]", 
      to = c("[email protected]", "[email protected]"), 
      subject = "Subject of the email", 
      body = "Body of the email", 
      smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE), 
      authenticate = TRUE, 
      send = TRUE, 
      attach.files = c("./download.log", "upload.log", "https://dl.dropboxusercontent.com/u/5031586/How%20to%20use%20the%20Public%20folder.rtf"), 
      file.names = c("Download log.log", "Upload log.log", "DropBox File.rtf"), # optional parameter 
      file.descriptions = c("Description for download log", "Description for upload log", "DropBox File"), # optional parameter 
      debug = TRUE) 

Lưu ý: máy chủ smtp của bạn có thể tìm thấy sử dụng quá đáng ngờ. Đây là trường hợp với ví dụ: gmail. Vì vậy, sau khi gửi một vài thư, bạn có thể phải đăng nhập vào gmail account và xem liệu tài khoản đã tạm thời bị vô hiệu hay chưa. Cũng lưu ý rằng nếu bạn sử dụng tài khoản gmail có xác thực hai yếu tố, bạn cần sử dụng an application specific password.

+0

Có lẽ bạn có thể bao gồm một ví dụ từ tài liệu để chứng minh mã sẽ trông như thế nào. Các câu trả lời khác đã bao gồm thành công các ví dụ. – BenBarnes

+0

downvotes? Hấp dẫn? – Andreas

+0

Không phải của tôi downvotes. – BenBarnes

Các vấn đề liên quan