// 邮件发送第三方

// 0. 下载第三方 nodemailer
// 1. 导入第三方
const nodemailer = require('nodemailer')

// 2. 按照规则使用
// 2-1. 创建邮差(配置发送方配置项)
// 语法: nodemailer.cretaeTransport({ 配置项 })
// 返回值: 一个邮差(右键发送器)
const transporter = nodemailer.createTransport({
  // 找到 node_modules -> nodemailer -> lib -> well-known -> servies.json 文件
  // 按照你的发送方地址查找
  "host": "smtp.qq.com",
  "port": 465,
  "secure": true,
  // 你的邮箱信息
  auth: {
    user: 'bbbb@qq.com', // 你的邮箱
    pass: 'cccc'  // smtp 允许的密码(不是邮箱登录密码)
  }
})

// 2-2. 利用邮差把邮件发送出去
// 语法: 邮差.sendMail({ 邮件信息配置项 }, 回调函数)
transporter.sendMail({
  from: 'aaa@qq.com',
  // 可以以字符串的形式配置一个目标邮箱, 可以以数组的形式配置一堆邮箱
  to: [ 'xxx1@qq.com', 'xxx2@qq.com', 'xxx3@qq.com', 'xxx4@qq.com', 'xxx5@qq.com' ],
  subject: '啥都有科技股份有限公司 - 登录验证码',
  // 注意: text 和 html 只能有一个
  // text: '文件内容',
  html: `
    <h1>您好, 这里是晨锋在线</h1>
    <h2>您本次登录的验证码是 <span style="color: red;">1234</span>, 十分钟内有效</h2>
    <h2>再见</h2>
  `
}, function (err, data) {
  if (err) return console.log('失败啦 : ', err)
  console.log('发送成功 : ', data)
})
posted on 2023-02-24 10:34  文种玉  阅读(74)  评论(0编辑  收藏  举报