21

我正在尝试使用以下代码在 Windows 上运行 sendmailR:

## Not run: 
from <- "<tal.galili@gmail.com>" # sprintf("<sendmailR@\\%s>", Sys.info()[4])
to <- "<tal.galili@gmail.com>"
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 tal.galili@gmail.com:statisfun:25 cannot be opened

这里的答案给出了Linux的解决方案,我将不胜感激Windows用户的建议。

谢谢。

4

4 回答 4

7

您可以试一试新的 mailR 包:http ://cran.r-project.org/web/packages/mailR/index.html

然后应该可以使用以下调用:

send.mail(from = "tal.galili@gmail.com",
          to = "tal.galili@gmail.com",
          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)
于 2014-03-11T21:34:51.587 回答
5

我曾经使用这些线路通过 R 发送电子邮件。

假设您的电子邮件tal.galili@gmail.com使用的是窗口操作系统(我的操作系统)

library(sendmailR)

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

# 2 case
from <- sprintf("<tal.galili@gmail.com>", Sys.info()[4]) 
to <- "<tal.galili@gmail.com>" 
subject <- "Hello from R" 
msg <- "my first email" 
sendmail(from, to, subject, msg,control=list(smtpServer="ASPMX.L.GOOGLE.COM")) 
于 2013-03-19T18:49:11.363 回答
1

每当 sendmailR 无法通过身份验证时,都会收到一条不太有用的消息,

Error in if (code == lcode) { : argument is of length zero

这可能有很多原因,包括服务器端的原因。就我而言,我需要将我的 IP 放在服务器的白名单上。@alko989 使用 sendemailR 声明有问题authentication ... is not supported by sendmailR并且截至 2015 年 2 月 20 日 sendmailR https://cran.r-project.org/web/packages/sendmailR/sendmailR.pdf的发布,唯一的控制参数是smtpServer, smtpPort& verbose,所以对于用户、密码、ssl、tls 等都没有。今天的邮件服务器往往比过去的邮件服务器更安全,所以这是 sendmailR 的一个严重限制。

于 2018-12-07T17:58:34.463 回答
0

作为使用 sendmailR 的替代方法,您可以尝试以下操作:

一起解析一个 VB 脚本(参见例如http://www.paulsadowski.com/wsh/cdo.htm),然后通过 shell 调用它。

这可能看起来像这样:

SendMail <- function(from="me@my-server.de",to="me@my-server.de",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="me.myself@andI.com",
    to="whatsup@man.com",
    text="Hallo",   
    subject="readThis",
    smtp="smtp.andI.com",
    user="me.myself@andI.com",
    pw="123456"
    )
于 2013-05-05T10:20:28.350 回答