Aquí hay un ejemplo que está configurado para un trabajo por lotes todos los días como el establecimiento utilizando Sendmail() en R (disponible con el paquete sendmailR) con varios archivos adjuntos (uno CSV, un PDF):
Configuración información de la fecha de referencia en los nombres de archivo:
> yesterday_date_stuff <- new.env()
> yesterday_date_stuff[['month']] <- strftime(Sys.Date()-1, format="%m")
> yesterday_date_stuff[['day']] <- strftime(Sys.Date()-1, format="%d")
> yesterday_date_stuff[['year']] <- strftime(Sys.Date()-1, format="%y")
> yesterday_date_stuff$month
[1] "03"
> yesterday_date_stuff$day
[1] "29"
> yesterday_date_stuff$year
[1] "17"
cree ahora parte de la información necesaria para sendmail() función al final de este post:
> from <- "[email protected]"
> to <- c("[email protected]", "[email protected]", "[email protected]")
> subject <- paste("whatever you want subject line to read for batch job analyzing data for ", yesterday_date_stuff$month, "/", yesterday_date_stuff$day, "/", yesterday_date_stuff$year, sep="")
> body <- "Text to insert into the body of your email"
Especificar servidor de correo aquí:
> mailControl=list(smtpServer="mail.whateveryourmailserveris.com")
Definir archivo adjunto 1 ruta y nombre:
> attachmentPath1 <- paste("file1name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".csv", sep="")
> attachmentName1 <- paste("file1name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".csv", sep="")
Definir archivo adjunto 1 objeto:
> attachmentObject1 <- mime_part(x=attachmentPath1,name=attachmentName1)
Definir archivo adjunto 2 ruta y nombre:
> attachmentPath2 <- paste("file2name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".pdf", sep="")
> attachmentName2 <- paste("file2name", "_", yesterday_date_stuff$month, yesterday_date_stuff$day, yesterday_date_stuff$year, ".pdf", sep="")
Definir el anexo 2 del objeto:
> attachmentObject2 <- mime_part(x=attachmentPath2,name=attachmentName2)
combinan ahora el cuerpo del correo electrónico con archivos adjuntos:
> bodyWithAttachment <- list(body,attachmentObject1, attachmentObject2)
> bodyWithAttachment
[[1]]
[1] "Text to insert into the body of your email"
[[2]]
<environment: 0x000000004efff188>
attr(,"class")
[1] "mime_part"
[[3]]
<environment: 0x00000000407a1b68>
attr(,"class")
[1] "mime_part"
enviar el correo electrónico usando sendmail función():
> sendmail(from=from, to=to, subject=subject, msg=bodyWithAttachment, control=mailControl)
añadí código para manejar múltiples archivos adjuntos aquí: http://stackoverflow.com/questions/2885660/how-to-send-email-with-attachment-from-r-in-windows/9131917#9131917 – ARobertson