使用R中的mailr包将文本消息和html表作为消息体发送

时间:2018-06-15 10:44:08

标签: r

send.mail(from = "abc@gmail.com",
          to = c("abc@gmail.com"),
          subject = mail_subject,
          body = "High_loss_gain_Imprsn_accounts.html",
          html=TRUE,
          attach.files = "c:/users/rkathuria/Documents/ACCOUNT_BLOCK_NO_COST_MONITOR.xlsx",
          smtp = list(host.name = "aspmx.l.google.com", port = 25),
          authenticate = FALSE,
          send = TRUE)

send.mail;的正文部分中,我想发送此html表和“Hello”消息。但是,它要么是我的消息,要么是html表。

body = "High_loss_gain_Imprsn_accounts.html" ---->此行在邮件正文中打印我的表。 body = "Hello" - >这行打印Hello。

如何将邮件组合在一起? < ------------------------------------------------ -------------------------------> 如果不使用xtable,我使用tableHTML包并编写我的代码,它是否会解决我在主题邮件上添加2个表的目的。

mail_body1<-tableHTML(High_loss_gain_Imprsn_accounts, widths = rep(100, 11), caption="Hi, High gain loss account", collapse = 'separate')
mail_body<-paste0(mail_body1,mail_body1)
mail_subject<-paste("Account Block No Cost Monitor ", Sys.Date()-1)

send.mail(from = "abc@gmail.com",
          to = c("abc@gmail.com"),
          subject = mail_subject,
          body = mail_body,
          html=TRUE,
          attach.files = "c:/users/rkathuria/Documents/ACCOUNT_BLOCK_NO_COST_MONITOR.xlsx",
          smtp = list(host.name = "aspmx.l.google.com", port = 25, ssl = TRUE),
          authenticate = FALSE,
      send = TRUE)

我面临的新问题是通过这个smtp,邮件现在还没有。

1 个答案:

答案 0 :(得分:0)

我之前已经解决了这个问题,我能找到的最佳解决方案是使用xtable生成带有标题的html表:

table <- data.frame(a = LETTERS[1:6],
           b = LETTERS[7:12])
mailR::send.mail(from = "XXXXXXXX",
                 to = "XXXXXXXX",
                 subject = "Hello World",
                 body = xtable::print.xtable(xtable::xtable(table, 
                                                            caption = "Hello World"), 
                                             type = "html", 
                                             caption.placement = "top",
                                             include.rownames = FALSE),
                 html = TRUE,
                 smtp = list(host.name = "smtp.gmail.com", 
                             port = 465, 
                             user.name = "XXXXXXXX",            
                             passwd = "XXXXXXXX", 
                             ssl = TRUE),
                 authenticate = TRUE,
                 send = TRUE)

问题是您只能将一个对象或路径传递给body。因此,如果你想拥有多个字幕行,你需要更改你的html对象,我想(我还没有测试过如果你在标题中添加更多文本会发生什么,所以这也可能有用)。