Use sendmailR with Windows
Asked Answered
G

4

21

I'm trying to run sendmailR on Windows with the following code:

## Not run: 
from <- "<[email protected]>" # sprintf("<sendmailR@\\%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."))

And get the following 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) :
  smtp.gmail.com [email protected]:statisfun:25 cannot be opened

The answer here give a solution for Linux, and I would be grateful for advice for Windows users.

Thanks.

Gernhard answered 14/3, 2013 at 22:7 Comment(2)
This doesn't answer your question but as an alternative you can use my GitHub gmailR that I know works with windows and is for gmail. It's something I canned as a package for personal use but isn't my work.Confederate
Nice Tyler - thank you. :) p.s: I'd still be interested in figuring out this sendmailR problem...Gernhard
P
7

You could give the new mailR package a shot: http://cran.r-project.org/web/packages/mailR/index.html

The following call should then work:

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)
Philippeville answered 11/3, 2014 at 21:34 Comment(2)
I still get an error. I was getting the same error as the OP and now when I tried this i get Check documentation to include all mandatory parameters to establisg SMTP connection. ... Yes. It said establisg not establish...Majormajordomo
mailR uses rJava ... which means you need to get that one working firstKrystenkrystin
H
5

I used to send emails via R using these lines.

Suppose your email is [email protected] using window OS (my operation system)

library(sendmailR)

# 1 case
from <- sprintf("<sendmailR@%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")) 
Humiliate answered 19/3, 2013 at 18:49 Comment(4)
Hi Hianni. In both cases I get: Error in wait_for(250) : SMTP Error: 5.7.1 [37.142.250.150] The IP you're using to send mail is not authorized toGernhard
Hey @TalGalili is strange I tested and i didn't get any error message, sorry.Humiliate
@TalGalili sorry if i cannot be more usefulHumiliate
I appreciate your attempt :)Gernhard
C
1

Any time that sendmailR fails to authenticate, one gets the not so helpful message that

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

This can be for many reasons, including server side reasons. In my case, I needed to put my IP on the server's whitelist. @alko989 declares at issue using sendemailR that authentication ... is not supported by sendmailR, and as of the 2015-Feb-20 publishing of sendmailR https://cran.r-project.org/web/packages/sendmailR/sendmailR.pdf, the only control parameters are smtpServer, smtpPort & verbose, so nothing for user, password, ssl, tls, etc. Mail servers today tend to be much more secure than the mail servers of the past, so that's a serious limitation of sendmailR.

Carlyn answered 7/12, 2018 at 17:58 Comment(1)
I know this can occur for many reasons but a common one I've experienced (well it happened twice anyway!) is that IT have renamed the the mail server you probably have hard-coded into your script for the 'control' argument of the sendmail function.Aegaeon
T
0

As an alternative to using sendmailR you might try this:

Parse together a VB-Script (see e.g. http://www.paulsadowski.com/wsh/cdo.htm ) and then call it via shell.

This might look like this:

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

... and use it like this:

SendMail(
    from="[email protected]",
    to="[email protected]",
    text="Hallo",   
    subject="readThis",
    smtp="smtp.andI.com",
    user="[email protected]",
    pw="123456"
    )
Trantrance answered 5/5, 2013 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.