步骤:
一、安装 nodemailer npm install nodemailer –save
二、使用createTransporter创建连接
三、编写邮件内容,邮件各个属性的意义看注释
四、利用transport.sendMail函数来发送邮件
服务端代码:
- const nodemailer = require(“nodemailer”);
- async function mail(to, title, html, files, filePath) {
- // 邮箱配置
- let transporter = nodemailer.createTransport({
- service: ‘163’,
- host: ‘smtp.163.com’,
- port: 465,
- secureConnection: true,
- auth: {
- user: ‘xxxxxx@163.com’,
- pass: ‘xxxxxx’
- }
- });
- // 邮件配置
- let info = await transporter.sendMail({
- from: ‘xxxxxx@163.com’, // 需与账号一致
- to: to,
- subject: title,
- html: html,
- attachments: files
- });
- }
- module.exports = mail;