Nodemailer

团队需要我做一个解析测试报告的程序,将最终结果发邮件给所有相关同事,前期自动化测试都是基于jenkins+gitlab+newman,发送邮件使用的是jenkins配置邮件插件。
这次需要借助非jenkins发送邮件,找了很久Nodemailer比较适合。

1
npm i nodemailer
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
var nodemailer = require("nodemailer");
// var smtpTransport = require('nodemailer-smtp-transport');

describe('', () => {
it.skip('发送邮件', function () {

var transport = nodemailer.createTransport({
host: "smtp.qq.com",// SMTP 端口
secureConnection: true,// 使用 SSL
port: 465,
auth: {
user: "liyinchi@qq.com",//邮箱账号
//这里密码不是qq密码,是你设置的smtp密码
pass: ""//授权码
}
});

var mailOptions = {
from: "liyinchi@qq.com",// 发件地址
to: "yinchi.li@xxxx.com",// 收件列表
// Subject of the message 邮件主题
subject: 'Nodemailer is unicode friendly ✔' + Date.now(),// 标题
//text和html两者只支持一种
// 标题
text: 'Hello to myself!',
// html 内容
html: `<p><b>Hello</b> to myself <img src="cid:note@example.com"/></p>
<p>Here's a nyan cat for you as an embedded attachment:<br/><img src="https://www.baidu.com/s?rsv_idx=1&wd=foreach&ie=utf-8&rsv_cq=js+%E6%88%AA%E5%8F%96%E6%95%B0%E7%BB%84&rsv_dl=0_right_recommends_merge_28335&euri=262673"/></p>`,
// An array of attachments
attachments: [
// String attachment 文本附件
{
filename: 'notes.txt', //附件名称,文本格式
content: '这是在notes.txt的内容', //文本中的内容
contentType: 'text/plain' // 可选,将从文件名中检测到
},
// File Stream attachment
{
filename: 'cat.jpg',
path: '/Users/liyinchi/workspace/前端/vue/test-file-management-platform/back-end/assets/nyan.jpg',
}
],
}

transport.sendMail(mailOptions, function (err, info,response) {
if (err) {
console.log(err);
console.log(info.accepted);
} else {
console.log(response);
}
})
})


});