使用R语言发送邮件

使用R语言发送邮件

终于发现了这个又好用又强大的发邮件的R包:blastula。赶紧学习一下。

安装

显然这个包是通过调用命令行工具发送邮件的,因此,首先安装这个命令行工具:

Shell
1
$brew install openssl

然后是安装R包:

R
1
devtools::install_github('rich-iannone/blastula')

使用

发邮件的过程主要需要四个函数:

  • compose_email(): 生成邮件内容;
  • preview_email(): 通过RStudio的Viewer展示由compose_email()生成的邮件内容。
  • send_email_out(): 发送基于HTML的邮件给一个或多个接收者。
  • create_email_creds_file(): 选择性的在本地创建一个邮件帐户。

除此之外还有一些帮助函数:

  • add_image(): 在邮件里添加一个本地图片。
  • add_readable_time(): 添加当前时间。
  • add_cta_buttom(): 添加一个带文本和链接的call-to-action按钮。
  • add_ggplot(): 添加一个ggplot绘图对象。

下面是一个简单的示例:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
library(magrittr)
library(blastula)
# 当前时间:
(current_date_time <- add_readable_time())

# 图片链接
img_link <- "https://marketplace.canva.com/MAA_AbacFmo/2/0/thumbnail_large/canva-basic-good-vibes-email-header-MAA_AbacFmo.jpg"

# 使用Markdown编写邮件正文
email_object <- compose_email(
body = "
## Hiya! This is an email message. Exciting Right?
Enjoy it. And this here image:

![The alt text]({img_link} \"The title\")

**Yeah!** I seriously hope that you enjoy this \\
message and the good vibes it will bring to you \\
and yours.

Peace out,

{sender}",

footer = "Brought to you by Smile AG on {current_date_time}",

sender = "Mike"
)

# 预览邮件
preview_email(email = email_object)

下面再创建邮件许可证,这个许可证会被存储在~/.e_creds中:

R
1
2
3
4
5
6
7
8
create_email_creds_file(
user = "[email protected]",
password = "(隐藏起来)",
host = "smtp.sina.com",
port = 465,
sender = "[email protected]",
creds_file_name = "~/.e_creds"
)

注意上面的host和port都是针对新浪邮箱的,不同邮箱运营商的host和port是不同的。具体可以参考这篇博客:常用的邮箱服务器(SMTP、POP3)地址、端口

最后发送邮件:

R
1
2
3
4
5
6
7
send_email_out(
message = email_object,
from = "[email protected]",
to = "[email protected]",
subject = "这是一封测试邮件",
creds_file = "~/.e_creds"
)

在邮件中添加HTML表格

formattable包生成的表格可以被直接添加入邮件中:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
library(formattable)
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE
)

# 使用format_table()创建HTML表格
formatted_table <-
format_table(
x = df,
list(
age = color_tile("white", "orange"),
grade =
formatter(
"span",
style = x ~ ifelse(
x == "A",
style(color = "green", font.weight = "bold"),
NA
)
),
area(col = c(test1_score, test2_score)) ~
normalize_bar("pink", 0.2),
final_score = formatter(
"span",
style = x ~ style(color = ifelse(rank(-x) <= 3,
"green", "grey")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))
),
regitered =
formatter(
"span",
style = x ~ style(color = ifelse(x, "green", "red")),
x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No"))
)
)
)

# 创建邮件正文:

email_object <- compose_email(
body = "
Hello,

Here are the grades you've been hounding me \\
for all this past week. Overall, everyone did \\
quite well. I'm impressed.

{formatted_table}
<br />
Cheers,<br />The grader"
)

preview_email(email_object)

# 发送

send_email_out(
message = email_object,
from = "[email protected]",
to = "[email protected]",
subject = "这是一封含表格的测试邮件",
creds_file = "~/.e_creds"
)

添加按钮

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cta_button <- add_cta_button(
url = "https://www.czxa.top",
text = "欢迎访问我的博客",
align = "center"
)

compose_email(
body = "
Hello!

Below is a call. It's a call to \\
action. Press it!

{cta_button}

(I really hope you press it.)

Cheers
") %>%
preview_email()

将本地图片添加到邮件中

以这张图片为例:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
img_file_html <- add_image(
file = 'WechatIMG23.jpeg'
)

# 构造邮件
email_object <-
compose_email(
body = "
Hello!

Take a look at this image:

{img_file_html}

It is of color bars.
"
)
preview_email(email_object)

send_email_out(
message = email_object,
from = "[email protected]",
to = "[email protected]",
subject = "这是一封含图片的测试邮件",
creds_file = "~/.e_creds"
)

手机端接收效果:

插入ggplot对象

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
library(ggplot2)
plot <-
ggplot(
data = mtcars,
aes(
x = disp, y = hp,
color = wt, size = mpg)) +
geom_point()

compose_email(
body = "
Hello!

Take a look at this plot:

{add_ggplot(plot_object = plot, width = 5, height = 5)}

It's a nice plot.

Cheers
") %>%
preview_email()

为我的网站写一个订阅邮件

这个用到啦tidyRSS包,这个包可以直接整理RSS。

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
library(tidyRSS)
library(tidyverse)
library(lubridate)
rss <- tidyfeed("https://www.czxa.top/atom.xml")

rss %<>% select(item_title,
item_link,
item_date_updated) %>%
mutate(
item_date_updated = item_date_updated %>% ymd_hms()
) %>%
arrange(desc(item_date_updated)) %>%
transmute(
标题 = paste0("<a href='", item_link, "'>", item_title, "</a>"),
最近更新日期 = item_date_updated
) %>%
slice(1:10)

(rss_table <- formattable(
x = rss
))

# 写邮件正文

# 今天的日期
todaydate <- Sys.time() %>%
as.character() %>%
substr(1, 10)

# 今天的星期
(week <- Sys.time() %>%
as.character() %>%
substr(1, 10) %>%
ymd() %>%
wday())
(week <- ifelse(week == 1, "星期日",
ifelse(week == 2, "星期一",
ifelse(week == 3, "星期二",
ifelse(week == 4, "星期三",
ifelse(week == 5, "星期四",
ifelse(week == 6, "星期五",
ifelse(week == 7, "星期六"))))))))

# 网站运行的时间
(durations <- (ymd(today()) - ymd("2017-09-29")) %>%
as.character())

compose_email(
body = "
## 您好!欢迎订阅我的博客!
今天是{todaydate},{week},自从2017年9月29日第一次建好个人网站以来,我的个人网站已经平稳的运行{durations}天啦!
最近我的网站更新的博客有:

{rss_table}
",
footer = "©️ 2018 程振兴</a>"
) %>% preview_email()

还是挺丑的。

由于travis支持R语言,我想其实也是可以用travis部署一下,然后就能做出来一个每日自动发邮件的程序啦!

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×