如何使用R中的MailR调整邮件正文

时间:2018-02-26 19:41:52

标签: python r dplyr tidyr sendmailr

我是新手使用Library mailR并在google上搜索很多但是找不到任何有用的东西。

我在R中有2个html表,我想在邮件正文中使用MailR库发送它们但是当我发送邮件时,两个表看起来都是相互连接的。

如何使用MailR库调整邮件正文。

我想要邮件看起来像下面提到的格式。

Hi Team,
"some sentence here" using t.start and nrows() in the sentence

enter image description here

空间

enter image description here

"some sentence here:

我正在使用代码来创建这样的表:

Table1<-DF%>% tableHTML(rownames = FALSE,
                      widths = rep(100, 13),
                      second_headers = list(c(1, 6, 6),
                                            c("", "ABC", "XYX")),
                      caption = "ABC Stat") %>%
  add_css_caption(css = list(c("font-weight", "border"),
                             c("bold", "1px solid black")))%>% add_css_row(css = list(c("background-color"), c("lightblue")), rows = 0:2)%>%add_css_caption(css = list(c("background-color"), c("lightblue")))

和table2的相同代码

要发送电子邮件,代码如下:

library(mailR)
sender<-"a12db@gmail.com"


recipients<-c("xyc@gmail.com")

sm<-list(host.name = "smmm.gmail.com", port = 123,
         user.name="a12db@gmail.com",
         passwd="1#4$12#", ssl=TRUE)

send.mail(from=sender,
          to=recipients,
          subject = paste0("Abc Repo"),
          body = paste("Some sentence",Table1,"\br",Table2),
          html = TRUE,
          inline = FALSE,
          smtp = sm,
          authenticate = TRUE,
          attach.files =("abc.csv") ,
          send = TRUE )

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

paragraphs <- list(
  glue::glue("Some sentence with {nrow(iris)}:"),
  tab1 = iris[1:3,1:3],
  tab2 = iris[1:3,3:5],
  glue::glue("And another sentence")
)
html <- do.call(paste, c(lapply(paragraphs, knitr::kable, format = "html"), list(sep="<br>")))
smtp <- list(
  host.name = "smtp.gmail.com", 
  port = 465, 
  user.name = "....", 
  passwd = "...", 
  ssl = TRUE
)
from <- to <- "....."
library(mailR)
send.mail(from = from,
          to = to,
          subject = "Report",
          html = TRUE,
          body = html,
          smtp = smtp,
          authenticate = TRUE,
          send = TRUE)  

enter image description here