使用node发送邮件使用到 nodemailer
安装
npm install nodemailer --save
# or
yarn add nodemailer
使用
const nodemailer = require('nodemailer') // 引入
// 配置发送邮件的对象
const transporter = nodemailer.createTransport({
// 默认支持的邮箱服务包括:”QQ”、”163”、”126”、”iCloud”、”Hotmail”、”Yahoo”等
service: "QQ",
auth: {
// 发件人邮箱账号
user: 'xxx@qq.com',
//发件人邮箱的授权码 需要在自己的邮箱设置中生成,并不是邮件的登录密码
pass: '*******'
}
})
// 配置收件人信息
const receiver = {
// 发件人 邮箱 '昵称<发件人邮箱>'
from: `"小明"<xxx@qq.com>`,
// 主题
subject: '录取通知',
// 收件人 的邮箱 可以是其他邮箱 不一定是qq邮箱
to: 'yyy@xxx.com',
// 可以使用html标签
html: `
<h1>在这里我们可以使用Html标签</h1>
<br>
<p>这是一段文字</p>
`
}
// 发送邮件
transporter.sendMail(receiver, (error, info) => {
if (error) {
return console.log('发送失败:', error);
}
transporter.close()
console.log('发送成功:', info.response)
})
运行项目 ,不出意外的话,收件人这时候就会收到一个邮件