nodejs
發送郵件
- 安裝
nodemailer
庫.
npm i nodemailer
- 配置發送方的服務器,賬號和密碼
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',
},
});
- 發送郵件
/**
* 發送指定主題以及內容給指定郵箱
* @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 服務打開,我們用的該服務,然後點擊生成授權碼生成。