中文版本地址:http://liangtongzhuo.com/atricle.html?5ac02cdaa22b9d00453c5e1b
Preface
In order to practice English, try to write articles in English. Syntax error, please correct me Email: liangtongzhuo@gmail.com
Work with a task that needs to generate exceles based on certain data and send them to the specified e-mail.
Generating Excel
Library “exceljs” used.
It’s very powerful, and l only use a small part of the functionality.
The code is as follows.
'use strict'
const Excel = require('exceljs');
var workbook = new Excel.Workbook();
// 创建页面
var ws1 = workbook.addWorksheet('名字');
ws1.addRow(["订单号", "商品"]);
// 标题颜色
['A1', 'B1', 'C1'].forEach((name, index) => {
ws1.getCell(name).fill = {
type: "pattern",
pattern: "solid",
fgColor: { argb: "4682B4FF" }
};
ws1.getCell(name).font = {
color: { argb: 'FFFFFFFF' },
bold: true
};
});
// 每一个标题的宽度
ws1.getColumn(1).width = 20;
ws1.getColumn(2).width = 15;
ws1.addRow(['11111', '22222']);
workbook.xlsx.writeFile('文件名字')
.then(()=>{
});
Send Email
Library “nodemailer” used.
We just call the interface through the mail server sending Email.
Building Email Server
The code is as follows.
const nodemailer = require('nodemailer');
const sendController = nodemailer.createTransport({
service: 'qq', // 使用了内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/
port: 465, // SMTP 端口
secureConnection: true, // 使用了 SSL
auth: {
user: '账号',
pass: '不是 qq 密码,设置的 smtp 授权码'
}
});
const mailOptions = {
from: `"系统自动发送" <'发送邮箱地址'>`,
to: '收件人的邮箱,“,” 隔开可以发送多人 ',
subject: `子标题`,
html: `<b>内容</b>`
// 附件路径
attachments: [{
filename: '文件名字',
path: '发送的文件路径'
}]
};
sendController.sendMail(mailOptions, (error, info) => {
});