Sospecho fuertemente que la respuesta más votada será "esa es la herramienta incorrecta para el trabajo". Reconozco que R puede no ser particularmente adecuado para enviar y recibir correos electrónicos, pero es el lenguaje de guiones que mejor conozco. Espero encontrar una forma de enviar y recibir correos electrónicos cortos en R. ¿Alguien sabe una forma establecida de hacerlo en una plataforma Windows? Podría usar una combinación de BLAT y GetMail, pero se preferiría una solución R nativa.¿Cómo puedo enviar/recibir (SMTP/POP3) correo electrónico usando R?
Editar: Una solución aceptable debe poder interactuar con los servidores que requieren SSL.
Editar 2: Proporciono mi puñalada en una respuesta del 80%. Lamentablemente, no se demuestra una forma nativa. En su lugar, utilizo una combinación profana de llamadas al sistema y programas de línea de comandos que probablemente no sea compatible en todas las plataformas. R llamadas nativas requerirán profundizar en la forma en que los servidores POP3 les gusta hablar con los clientes conectados y una comprensión de SSL que no tengo actualmente. Otras respuestas son aún alentadas.
##Note: Other programs are wrapped in R functions and system calls.
#They each have their own licenses which may or may not allow the use suggested here
#Programs used here:
#STunnel: http://www.stunnel.org/; Provides an SSL tunnel but requires OpenSSL
#OpenSSL: http://www.openssl.org/; OpenSSL to actually provide SSL
# Note that these .dlls should be placed with the stunnel exe.
# Also note that libssl32.dll may need to be renamed from ssleay32.dll
#Microsoft Visual C++ 2008 Redistributable (may be required for the SSL .dlls to work correctly)
#Blat: http://www.blat.net; a public domain SMTP sending program
#Getmail is free for non-commercial use. If you use it in a business environment, then a fee of $50 USD is payable to Tim Charron.
#Stunnel is a TSR, so it will need to be killed from the task manager if there is an issue. If you are willing to install it as a service you may be able to tweak my code to start and stop the service.
#My current code does not create .conf file for stunnel the way a full version ought. Check http://spampal.sanesecurity.com/manual_eng/servers/stunnel/stunnel.htm#sconfig21 to create the appropriate configuration file.
#Set the config values as appropriate
##Config##
BLAT.loc <- "c:/Programming/R/Rmail/blat262/full/blat.exe"
GetMail.loc <- "C:/Programming/R/RMail/getmail133/getmail.exe"
stunnel.loc <- "C:/Programming/R/RMail/stunnel/stunnel-4.11.exe"
#The set mail function assigns the username and password to be used as well as the smtp and pop3 servers it starts stunnel (and assumes that the stunnel.conf file is present and set correctly).
setMail <- function(user,pw,SSL=FALSE,smtp="127.0.0.1:259",pop3="127.0.0.1:1109")
{
if (SSL==TRUE)
{
print("Starting stunnel; you will need to kill this from the task-manager")
system(stunnel.loc,wait=FALSE)
Sys.sleep(2) #Give it time to start
}
return(list(user=user,pw=pw,smtp=smtp,pop3=pop3,SSL=SSL))
}
#function to send mail, myMail is the resulting list from setMail
sendmail <- function(myMail, to, subject, msg,VERBOSE=FALSE)
{
writeLines(msg, "out.txt", sep = "\n", useBytes = FALSE)
targ <- paste(getwd(),"/out.txt",sep="")
call <- paste(BLAT.loc, ' "',targ,'" -subject "',subject,'" -to ',to," -u ",myMail$user," -pw ",myMail$pw, " -f ",myMail$user, " -debug -server ",myMail$smtp,sep="")
res <- system(call,intern=TRUE)
if (VERBOSE) {return(res)}
}
#function to get mail, myMail is the resulting list from setMail; it returns a list with one element that contains everything unparsed, another list provides the number of messages remaining on the server.
getmail <- function(myMail,VERBOSE=FALSE)
{
unlink("MSG1.txt") #drop previous get
#download next message
call <- paste(GetMail.loc," -u ",myMail$user," -pw ",myMail$pw," -s ",strsplit(myMail$pop3,":")[[1]][1],
" -port ",strsplit(myMail$pop3,":")[[1]][2]," -n 1",sep="")
res <- system(call,intern=TRUE)
if (VERBOSE) {print(res)}
nmsgtxt <- res[grep("messages on the server.",res)]
nstart <- regexpr("There are",nmsgtxt)
nend <- regexpr("messages on the server.",nmsgtxt)
nmess <- as.numeric(substr(nmsgtxt,10,nend-1))-1
x <- readLines("MSG1.txt",-1)
return(list(message=x,remaining=nmess))
}
caso de uso: En pocas palabras, tengo que tener R sea capaz de enviar un mensaje cuyo contenido se determina en el guión R en otro lugar a un servidor SMTP. Los participantes recibirán el correo electrónico y responderán a él. Necesito recuperar su respuesta de mi servidor POP3 y almacenarla en una estructura de datos R para que pueda realizar el procesamiento posterior en ella. En la práctica, estoy estableciendo una forma de hacer un muestreo de experiencia por medio de R. Es decir, R puede enviar un correo electrónico a un participante "¿Cómo estás hoy (1 = malo, 7 = excelente)?" El participante puede responder "4" y puedo hacer coincidir la pregunta formulada, la respuesta, etc. en una base de datos para el análisis estadístico.
similares (pero no una causa duplicado de SSL) http://stackoverflow.com/questions/2885660/how-to-send-email-with-attachment-from-r-in-windows y http://stackoverflow.com/questions/3572607/sendmailr-part2 -sending-files-as-mail-attachments – Marek
¿Qué pasa con el tono de Edit2 y Edit3? ¿Es realmente necesario? –
En una nota al margen, Perl (y por extensión Python) sigue siendo un lenguaje de scripting principal utilizado como back-end para muchos sitios web. Contiene una gran cantidad de funciones para enviar y recibir correos electrónicos (automatizados) (muchas listas de correo aprovechan estas funcionalidades en Perl). También soy programador por necesidad, pero estoy feliz de haber aprendido a trabajar con más de una "herramienta" (es decir, el lenguaje). A largo plazo, parece una mejor forma de hacerlo. Además, Perl y R pueden estar vinculados. O Python fuera de curso, también puede hacerlo. –