Voici un exemple qui est configuré pour un traitement par lots par jour comme le réglage en utilisant sendmail() dans R (disponible avec le package sendmailR) avec plusieurs pièces jointes (un CSV, un PDF):
Mise en place informations à jour pour référencer dans les noms de fichiers:
> 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"
créez maintenant une partie des informations nécessaires à la fonction sendmail() à la fin de ce 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"
Spécifier le serveur de mail:
> mailControl=list(smtpServer="mail.whateveryourmailserveris.com")
Définir l'annexe 1 chemin et le nom:
> 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="")
Define pièce jointe 1 objet:
> attachmentObject1 <- mime_part(x=attachmentPath1,name=attachmentName1)
Définir l'annexe 2 chemin et le nom:
> 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="")
Define pièce jointe 2 objet:
> attachmentObject2 <- mime_part(x=attachmentPath2,name=attachmentName2)
Maintenant moissonneuse-batteuse le corps de l'e-mail avec vos pièces jointes:
> 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"
Envoyer l'e-mail en utilisant la fonction sendmail():
> sendmail(from=from, to=to, subject=subject, msg=bodyWithAttachment, control=mailControl)
J'ai ajouté code pour gérer plusieurs pièces jointes ici: http://stackoverflow.com/questions/2885660/how-to-send-email-with-attachment-from-r-in-windows/9131917#9131917 – ARobertson