r - 如何将格式添加到mailR邮件正文
问题描述
我正在尝试发送有关使用mailR
包的邮件,但由于这是一个非常复杂的问题,我想添加一些格式:
- “不要砍伐这片森林!” 应该是粗体的
- 编号列表应该很好地格式化
.
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")
邮件正文实际上发送::
< 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"> ...
我需要个性化每封邮件,在第一个解决方案(那个
txt
)中,我使用了简单的gsub
功能,我相信它不会在这里工作。
我也尝试过手动添加到TXT
文件、html 格式(<b>
和<strong>
),但它没有用。
谢谢!
解决方案
创建一个 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)
推荐阅读
- java - ClassA 不能转换为 ClassA:java.lang.ClassCastException
- azure-devops - Azure Devops 中的仪表板查看测试结果的权限
- mysql - 尝试禁用外键检查,但不成功
- objective-c - 为什么不存在的外部变量的地址不等于null
- c# - 回滚后提交事务时会发生什么?
- python-3.x - 使用日期列将 excel 转换为 csv 无法按预期工作
- json - jq:迭代内部多个数组
- grpc - 如何对 grpc 服务进行负载测试,任何示例代码将不胜感激
- apache-spark - Univocity CSV Parser setDelimiter 方法的 Spark java.lang.NoSuchMethodError
- angular - 在 Angular 应用程序中定义和使用 xhook