如何将格式添加到mailR邮件正文

时间:2019-06-11 13:21:09

标签: r

我正在尝试发送有关使用mailR软件包的邮件,但是由于这是一个非常复杂的问题,我想加点形式:

  1. “不要砍伐这个森林!”应该是粗体
  2. 编号列表的格式应正确

library(mailR) # library used to send mails

# The text I would like to send:
Text <- "Hi! 

Do not cut this forest!

The reason for this  ar as follows:
1. Trees are good
2. bla bla bla

best regards,
MS"

#In reality I am reading it from TXT file
text_real <- readChar('text_real.txt', file.info('text_real.txt')$size)
text_real <- enc2utf8(text_real)

sender <- ...
recipients <- ...
password <- ...
title <- "title"


#Sending mail
send.mail(from = sender,
          to = recipients,
          subject = title,
          body = Text,
          encoding = "utf-8",
          smtp = list(host.name = "smtp.gmail.com", port = 465, 
                      user.name = sender,            
                      passwd = password, ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

或者,我知道mailR正在使用html,所以我都在World中编写了所有内容,并保存为html。不幸的是,它不起作用,并提出了第二个问题

Text <- paste(readLines("real text.htm"), collapse="\n")
  1. 邮件正文实际上发送::

    ...

  2. 在第一个解决方案(txt)中,我需要个性化每封邮件,我使用了简单的gsub函数,但我相信它在这里无法正常工作。

    < / li>

我还尝试过手动将html格式(TXT<b>)添加到<strong>文件中,但这没有用。

谢谢!

1 个答案:

答案 0 :(得分:1)

创建html是正确的,但是您没有将其读入R。send.mail()可以将html文件作为正文发送。

library(mailR)

send.mail(from = sender,
          to = recipients,
          subject = title,
          body = "path-to-html-file",  #pass the file
          html = TRUE,                #tell send.mail you're using html
          encoding = "utf-8",
          smtp = list(host.name = "smtp.gmail.com", port = 465, 
                      user.name = sender,            
                      passwd = password, ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)