Domanda

Seguendo le indicazioni fornite nel questa domanda relativa , sono stato in grado di inviare HTML formattato messaggi di posta elettronica. Ora la domanda è questa:? Come devo modificare il codice riportato di seguito, al fine di collegare uno o più file (di qualsiasi tipo) per questo messaggio

library(sendmailR)

from <- "<sendmailR@myserver.mycompany.com>"
to <- c("<someone@mycompany.com>","<anotherone@mycompany.com>")
subject <- iconv("Message Title", to = "utf8")

msg <- "<hr size='2' width='33%' style='text-align: left;'><font size='2'>
  <i>This email was sent automatically using <a href='http://finzi.psych.upenn.edu/R/library/sendmailR/html/00Index.html' rel='nofollow' target='_blank'>sendmailR</a>.<br>
  Please do not reply directly to this e-mail.</i></font>"

msg <- iconv(msg, to = "utf8")

sapply(to,function(x) sendmail(from, x, subject, msg, control=list(smtpServer="###.###.###.###"), headers=list("Content-Type"="text/html; charset=UTF-8; format=flowed")))
È stato utile?

Soluzione

Con il pacchetto mailR ( https://github.com/rpremraj/mailR ), è possibile inviare email HTML e inoltre allegare file con facilità, come di seguito:

send.mail(from = "sender@gmail.com",
          to = c("recipient1@gmail.com", "recipient2@gmail.com"),
          subject = "Subject of the email",
          body = "<html>The apache logo - <img src=\"http://www.apache.org/images/asf_logo_wide.gif\"></html>",
          html = TRUE,
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = TRUE),
          attach.files = c("./download.log", "upload.log"),
          authenticate = TRUE,
          send = TRUE)

Modifica (2014/05/13):

mailR è stato aggiornato per consentire diverse codifiche dei caratteri. Di seguito è riportato un esempio per inviare il messaggio come UTF-8.

send.mail(from = "Sender Name <sender@gmail.com>",
          to = "recipient@gmail.com",
          subject = "A quote from Gandhi",
          body = "In Hindi :  थोडा सा अभ्यास बहुत सारे उपदेशों से बेहतर है।
                  English translation: An ounce of practice is worth more than tons of preaching.",
          encoding = "utf-8",
          smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "gmail_username", passwd = "password", ssl = T),
          authenticate = TRUE,
          send = TRUE)

Altri suggerimenti

Un lavoro (almeno per me) la funzione:

sendMessage<-function(contents,subject,from,to,attMIME,attachment,control){    
   msg<-list(contents,sendmailR:::.file_attachment(attachment,attachment,attMIME));
   sendmail(from=from,to=to,subject=subject,msg=msg,control=control);
}

Può essere utilizzato in questo modo:

png('a.png');hist(rnorm(700));dev.off()
sendMessage('Here you have a nice histogram:',
'Nice picture',
'from@example.com',
'to@example.com',
'image/png',
'a.png',list(smtpServer="..."))

Sia avvertito che messaggio inviato da questo esempio sarà probabilmente contrassegnato come spam, in quanto si tratta di un breve testo e un grande quadro - tuttavia, per i messaggi più grandi e, diciamo, allegati PDF dovrebbe passare attraverso. In caso contrario, si può considerare l'aggiunta anche una versione testo del messaggio.

EDIT (meno rilevante ora): L'intuizione più profonda su come rendere i messaggi MIME può essere trovato qui .

Si noti che le versioni attuali di allegati di supporto sendmailR fuori dalla scatola rendendo msg un elenco di oggetti mime_type, vale a dire faresti ora

sendmail( from,to,subject,
          msg=list(mime_part("Here's an attachment for you!"), 
          mime_part(attachmentFileName)), control, headers)`

vorrei rinunciare a utilizzare R per questo. Lavorare, multipiattaforma, soluzioni stabili per fare questo in Python esiste, e si può chiamare Python da R.

Se dovessi montare un modello a effetti misti in un programma Python che definirei R farlo - se voglio fare un compito sistemi come inviare e-mail in R chiamerò Python per farlo. La sua pena di imparare, se non si sa ancora.

Ecco un esempio che è impostato per un processo batch quotidiano come impostazione usando sendmail () in R (disponibile con il pacchetto sendmailR) con più allegati (uno CSV, un PDF):

Impostazione delle informazioni data di riferimento nei nomi dei file:

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

Ora create alcune delle informazioni necessarie per sendmail () la funzione alla fine di questo post:

> from <- "youremail@whateveryourmailserveris.com"
> to <- c("person_A_to_send_email_to@whatever.com", "person_B_to_send_email_to@whatever.com", "person_C_to_send_email_to@whatever.com")
> 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"                     

Specifica server di posta qui:

> mailControl=list(smtpServer="mail.whateveryourmailserveris.com")

Definisci allegato 1 percorso e il nome:

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

Definire allegato 1 oggetto:

> attachmentObject1 <- mime_part(x=attachmentPath1,name=attachmentName1)

Definisci attaccamento 2 percorso e il nome:

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

Definire allegato 2 oggetto:

> attachmentObject2 <- mime_part(x=attachmentPath2,name=attachmentName2)

Ora si combinano il corpo della e-mail con allegati:

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

Invia l'e-mail utilizzando la funzione sendmail ():

> sendmail(from=from, to=to, subject=subject, msg=bodyWithAttachment, control=mailControl)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top