How to send HTML email using R

前端 未结 5 904
慢半拍i
慢半拍i 2020-12-16 02:44

I have searched SO and well as google and cannot seem to find a solution to my problem. I am trying to send a HTML formatted email within R using sendmailR package. The plai

相关标签:
5条回答
  • 2020-12-16 02:56

    I prefer to use a specialized Mail agent for this type of task than using an R package. You can for example use Mutt. Available for linux and windows.

    Here I am using option -e to send a command:

    writeLines(message,
               p<-pipe(paste('mutt -e ','"set content_type=text/html"',
                              from,to,' -s ', subject))
    close(p)
    
    0 讨论(0)
  • 2020-12-16 02:58

    With the mailR package (https://github.com/rpremraj/mailR), you could send HTML emails with ease as below:

    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)
    
    0 讨论(0)
  • 2020-12-16 03:06

    This is possible, cf https://stackoverflow.com/a/21930556/448145 Just add:

    msg <- mime_part(message)
    msg[["headers"]][["Content-Type"]] <- "text/html"
    sendmail(from, to, subject, msg = msg, ...)
    
    0 讨论(0)
  • 2020-12-16 03:08

    If nothing else, you can post the html to a php page, and have php send the html email. We'll see if anyone else has a better solutions.

    0 讨论(0)
  • 2020-12-16 03:10

    sendmailR cannot do this because it is hard-coded to send the message part out as text. If you look at the packages source, line 38 of sendmail.R is the following:

    writeLines("Content-Type: text/plain; format=flowed\r\n", sock, sep="\r\n")
    

    Change that to

    writeLines("Content-Type: text/html; format=flowed\r\n", sock, sep="\r\n")
    

    like you tried to do through the options and it will work.

    Update: sendmailR now allows html emails (see Karl's answer below and https://stackoverflow.com/a/21930556/448145).

    0 讨论(0)
提交回复
热议问题