Featured image of post 利用nodemailer发送邮件

利用nodemailer发送邮件

利用nodemailer发送邮件

利用nodemailer发送邮件

nodemailer是在nodejs中使用的发送邮件的模块。

官网地址

Nodemailer

如何使用

  1. 安装

    1
    
    npm install nodemailer
    
  2. 例子

    申请网易126邮箱,nodemailer2022@126.com

    打开设置,开启服务,开启服务的时候会有个授权密码,这个密码是用于第三方登录的授权密码

    Alt text

     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
    
    "use strict";
    const nodemailer = require("nodemailer");
    
    // 要使用await,必须在async函数中
    async function main() {
      // 创建传输对象
      let transporter = nodemailer.createTransport({
    	//填服务器主机
        host: "smtp.126.com",
        port: 25,
        secure: false, // true for 465, false for other ports
        auth: {
          user: 'nodemailer2022@126.com', // 邮箱账号
          pass: 'xxx', // 授权密码
        },
      });
    
      // 定义传输信息
      let info = await transporter.sendMail({
        from: 'nodemailer2022@126.com', // 发送邮件地址
        to: "bar@example.com, baz@example.com", // 接受邮箱列表
        subject: "Hello ✔", // 邮件主题
        text: "Hello world?", // 内容
        html: "<b>Hello world?</b>", // html内容
      });
    
      console.log("Message sent: %s", info.messageId);
      // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
    
      // Preview only available when sending through an Ethereal account
      console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
      // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
    }
    
    main().catch(console.error);
    

    案例:

Alt text

代码:
 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
  // 发送邮件
async sendMail() {
  const email = this.ctx.query.mail;
  const transporter = nodemailer.createTransport({
    host: 'smtp.126.com',
    port: 25,
    secure: false, // true for 465, false for other ports
    auth: {
      user: 'nodemailer2022@126.com', // generated ethereal user
      pass: 'xxx', // generated ethereal password
    },
  });
  // 随机产生验证码
  const code = await this.ctx.service.tool.code();
  this.ctx.session.emailCode = code;
  const info = await transporter.sendMail({
    from: 'nodemailer2022@126.com', // sender address
    to: email, // list of receivers
    subject: 'Hello ✔', // Subject line
    text: '您的验证码是' + code, // plain text body

  });

}
  

注册:

 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
// 注册
async register() {
  const postData = this.ctx.request.body;
  const username = postData.username;
  const result1 = await this.app.mysql.get('account', { username });
  if (result1) {
    this.ctx.body = {
      success: false,
      msg: '账号已经存在!',
    };
    return;
  }
  const code = this.ctx.session.emailCode;
  const result2 = code === postData.code - 0;
  if (!result2) {
    this.ctx.body = {
      success: false,
      msg: '邮箱验证码不正确!',
    };
    return;
  }
  // 调用service中的salt()方法获取随机的32位字符串
  const salt = await this.service.tool.salt();
  const password = this.ctx.request.body.password;
  const email = this.ctx.request.body.email;
  const myVerifier = computeVerifier(
    params.trinitycore,
    Buffer.from(salt),
    username.toUpperCase(),
    password.toUpperCase()
  );
  // 数据库中保存了email,username,verifier,salt
  const result = await this.app.mysql.insert('account', { email, salt, username, verifier: myVerifier });
  if (result.affectedRows === 1) {
    this.ctx.body = {
      success: true,
      msg: '账号注册成功!',
    };
    this.ctx.session.emailCode = null;
    return;
  }
  this.ctx.body = {
    success: false,
    msg: '账号注册失败!',
  };
}