發送郵件

nodejs發送郵件

  1. 安裝 nodemailer庫.
npm i nodemailer
  1. 配置發送方的服務器,賬號和密碼
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
  host: 'smtp.qq.com',
  port: 587,
  secure: false, // true for 465, false for other ports
  auth: {
    user: '[email protected]',
    pass: 'xxxxxxxxxxxx',
  },
});
  1. 發送郵件
  /**
 * 發送指定主題以及內容給指定郵箱
 * @param {string} to 接收者的郵箱
 * @param {string} subject 郵件主題
 * @param {string} body 郵件內容
 */
  async sendEmail(to, subject, body) {
    await transporter.sendMail({
      from: admin_email,
      to, subject,
      text: body,
      // html: '',
    });
  }

發送簡單郵件。
詳情

備註:

我發送方的郵箱用的是 QQ郵箱,其中密碼並不是QQ密碼而是授權碼,通過QQ郵箱裏獲得。

在郵箱設置,賬戶下:
在這裏插入圖片描述
在這裏插入圖片描述
然後把 SMTP 服務打開,我們用的該服務,然後點擊生成授權碼生成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章