issue using sendemailR

juniordataminer picture juniordataminer · May 21, 2014 · Viewed 8.4k times · Source

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!

Answer

alko989 picture alko989 · May 21, 2014

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 <- "[email protected]"
recipients <- c("[email protected]")
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="[email protected]", passwd="YOURPASSWORD", ssl=TRUE),
authenticate = TRUE,
send = TRUE)

Hope it helps,

alex