2013-03-14 29 views
18

我想用下面的代碼在Windows上運行sendmailR:使用sendmailR與Windows

## Not run: 
from <- "<[email protected]>" # sprintf("<[email protected]\\%s>", Sys.info()[4]) 
to <- "<[email protected]>" 
subject <- "Hello from R" 
body <- list("It works!", mime_part(iris)) 
sendmail(from, to, subject, body, 
     control=list(smtpServer="ASPMX.L.GOOGLE.COM.")) 

並得到以下錯誤:

Error in socketConnection(host = server, port = port, blocking = TRUE) : 
    cannot open the connection 
In addition: Warning message: 
In socketConnection(host = server, port = port, blocking = TRUE) : 
    smtp.gmail.com [email protected]:statisfun:25 cannot be opened 

The answer here給用於Linux的解決方案,我會很感激Windows用戶的建議。

謝謝。

+5

這不會回答你的問題,但作爲替代方案,你可以使用我的GitHub [gmailR](https://github.com/trinker/gmailR),我知道它適用於Windows,適用於gmail。這是我個人使用的包裝,但不是我的工作。 – 2013-03-14 23:51:31

+2

尼斯泰勒 - 謝謝。 :) p.s:我仍然有興趣弄清楚這個sendmailR問題... – 2013-03-15 07:46:26

回答

5

你可以給新mailR包一個鏡頭:http://cran.r-project.org/web/packages/mailR/index.html

以下電話應該工作:

send.mail(from = "[email protected]", 
      to = "[email protected]", 
      subject = "Subject of the email", 
      body = "Body of the email", 
      smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "tal.galili", passwd = "PASSWORD", ssl = TRUE), 
      authenticate = TRUE, 
      send = TRUE) 
+0

我仍然得到一個錯誤。我得到了與OP一樣的錯誤,現在當我嘗試這個時,我得到了'檢查文檔以包含所有必需的參數來建立SMTP連接。'...是的。它說establisg不建立... – NealC 2015-05-28 14:32:18

4

我曾經使用這些行通過R發送電子郵件。

假設你的電子郵件[email protected]使用窗口操作系統(我的操作系統)

library(sendmailR) 

# 1 case 
from <- sprintf("<[email protected]%s>", Sys.info()[4]) 
to <- "<[email protected]>" 
subject <- "Hello from R" 
msg <- "my first email" 
sendmail(from, to, subject, msg,control=list(smtpServer="ASPMX.L.GOOGLE.COM")) 

# 2 case 
from <- sprintf("<[email protected]>", Sys.info()[4]) 
to <- "<[email protected]>" 
subject <- "Hello from R" 
msg <- "my first email" 
sendmail(from, to, subject, msg,control=list(smtpServer="ASPMX.L.GOOGLE.COM")) 
+0

嗨Hianni。在這兩種情況下,我都會收到:Error in wait_for(250): SMTP錯誤:5.7.1 [37.142.250.150]您用來發送郵件的IP未授權給 – 2013-03-20 20:18:27

+1

嘿@TalGalili很奇怪我測試過,沒有任何錯誤信息,對不起。 – 2013-03-20 21:08:52

+1

公司,謝謝詹尼。 – 2013-03-21 15:59:43

0

作爲替代使用sendmailR你可以試試這個:

解析在一起的VB腳本(見例如http://www.paulsadowski.com/wsh/cdo.htm)然後通過shell調用它。

這可能是這樣的:

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

...並使用它像這樣:

SendMail(
    from="[email protected]", 
    to="[email protected]", 
    text="Hallo", 
    subject="readThis", 
    smtp="smtp.andI.com", 
    user="[email protected]", 
    pw="123456" 
    )