issue using sendemailR [duplicate]

北城余情 提交于 2020-01-02 10:16:38

问题


I am trying to use sendemailR package in R but I am getting an error I don't know how to fix.

When trying the default parameters:

library(sendmailR)
from <- "your_email"
to <- "your_email"
subject <- "Test send email in R"
body <- "It works!"                     
mailControl=list(smtpServer="smtp.gmail.com")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)

I get the error

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) :
Gmail SMTP Server:25 cannot be opened

So I change the port to 465 and it seems to work

library(sendmailR)
from <- "your_email"
to <- "your_email"
subject <- "Test send email in R"
body <- "It works!"                     
mailControl=list(smtpServer="smtp.gmail.com", smtpPort="465")
sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)

but then I get the following error

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

Any idea what's happening?

This is the version of R and Windows

R version 3.0.3 (2014-03-06) -- "Warm Puppy"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

Thanks!


回答1:


There are two things that need attention in your example:

As @David Arenburg commented, to should contain a valid email address.

The second thing is the smtp server you are using: smtp.gmail.com. This server need authentication which is not supported by sendmailR.

You can use an smtp server that does not require authentication (e.g. the restricted gmail smtp server: aspmx.l.google.com, port 25, see here for details)

The other option is to use the mailR package that allows authentication.

Try something like (of course you have to put valid email addresses and user.name and passwd to work):

library(mailR)
sender <- "SENDER@gmail.com"
recipients <- c("RECIPIENT@gmail.com")
send.mail(from = sender,
to = recipients,
subject="Subject of the email",
body = "Body of the email",
smtp = list(host.name = "smtp.gmail.com", port = 465, 
        user.name="YOURUSERNAME@gmail.com", passwd="YOURPASSWORD", ssl=TRUE),
authenticate = TRUE,
send = TRUE)

Hope it helps,

alex



来源:https://stackoverflow.com/questions/23792137/issue-using-sendemailr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!