首页 > 解决方案 > 如何将格式添加到mailR邮件正文

问题描述

我正在尝试发送有关使用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)

Alternativly 我知道它mailR正在使用html,所以我把它全部写在 World 中并保存为 html。不幸的是,它没有用,并引起了第二个问题

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

    < html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word " xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"> ...

  2. 我需要个性化每封邮件,在第一个解决方案(那个txt)中,我使用了简单的gsub功能,我相信它不会在这里工作。

我也尝试过手动添加到TXT文件、html 格式(<b><strong>),但它没有用。

谢谢!

标签: r

解决方案


创建一个 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)

推荐阅读