Nodemailer簡介
Nodemailer是一個簡單易用的Node.js郵件發(fā)送組件
官網(wǎng)地址:https://nodemailer.com
GitHub地址:https://github.com/nodemailer/nodemailer
Nodemailer的主要特點包括:
- 支持Unicode編碼
- 支持Window系統(tǒng)環(huán)境
- 支持HTML內(nèi)容和普通文本內(nèi)容
- 支持附件(傳送大附件)
- 支持HTML內(nèi)容中嵌入圖片
- 支持SSL/STARTTLS安全的郵件發(fā)送
- 支持內(nèi)置的transport方法和其他插件實現(xiàn)的transport方法
- 支持自定義插件處理消息
- 支持XOAUTH2登錄驗證
安裝使用:
1.先下載
npm install nodemailer --save
2.封裝sendEmail.js
這里我對發(fā)送郵件進行了簡單的封裝,用的時候只需要傳參數(shù)就好了
const nodemailer = require('nodemailer'); //引入模塊
let transporter = nodemailer.createTransport({
//node_modules/nodemailer/lib/well-known/services.json 查看相關的配置祟滴,如果使用qq郵箱,就查看qq郵箱的相關配置
service: 'qq', //類型qq郵箱
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'xxxx@qq.com', // 發(fā)送方的郵箱
pass: 'xxxx' // smtp 的授權碼
}
});
//pass 不是郵箱賬戶的密碼而是stmp的授權碼(必須是相應郵箱的stmp授權碼)
//郵箱---設置--賬戶--POP3/SMTP服務---開啟---獲取stmp授權碼
function sendMail(mail, code, call) {
// 發(fā)送的配置項
let mailOptions = {
from: '"Express-demo" <854453495@qq.com>', // 發(fā)送方
to: mail, //接收者郵箱例衍,多個郵箱用逗號間隔
subject: '歡迎來到"Express-demo"', // 標題
text: 'Hello world?', // 文本內(nèi)容
html: '<p>這里是"Express-demo"詳情請點擊:</p><a href="http://www.reibang.com/u/5cdc0352bf01">點擊跳轉</a>', //頁面內(nèi)容
// attachments: [{//發(fā)送文件
// filename: 'index.html', //文件名字
// path: './index.html' //文件路徑
// },
// {
// filename: 'sendEmail.js', //文件名字
// content: 'sendEmail.js' //文件路徑
// }
// ]
};
//發(fā)送函數(shù)
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
call(false)
} else {
call(true) //因為是異步 所有需要回調(diào)函數(shù)通知成功結果
}
});
}
module.exports = {
sendMail
}
3.使用方法
const express = require('express')
const app = express()
const email = require('./sendEmail.js'); //引入封裝好的函數(shù)
const bodyParser = require('body-parser')
const cors = require('cors'); // 解決跨域問題
const styles = {
'bold': ['\x1B[1m', '\x1B[22m'],
'italic': ['\x1B[3m', '\x1B[23m'],
'underline': ['\x1B[4m', '\x1B[24m'],
'inverse': ['\x1B[7m', '\x1B[27m'],
'strikethrough': ['\x1B[9m', '\x1B[29m'],
'white': ['\x1B[37m', '\x1B[39m'],
'grey': ['\x1B[90m', '\x1B[39m'],
'black': ['\x1B[30m', '\x1B[39m'],
'blue': ['\x1B[34m', '\x1B[39m'],
'cyan': ['\x1B[36m', '\x1B[39m'],
'green': ['\x1B[32m', '\x1B[39m'],
'magenta': ['\x1B[35m', '\x1B[39m'],
'red': ['\x1B[31m', '\x1B[39m'],
'yellow': ['\x1B[33m', '\x1B[39m'],
'whiteBG': ['\x1B[47m', '\x1B[49m'],
'greyBG': ['\x1B[49;5;8m', '\x1B[49m'],
'blackBG': ['\x1B[40m', '\x1B[49m'],
'blueBG': ['\x1B[44m', '\x1B[49m'],
'cyanBG': ['\x1B[46m', '\x1B[49m'],
'greenBG': ['\x1B[42m', '\x1B[49m'],
'magentaBG': ['\x1B[45m', '\x1B[49m'],
'redBG': ['\x1B[41m', '\x1B[49m'],
'yellowBG': ['\x1B[43m', '\x1B[49m']
};
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json())
app.use(cors());
app.use(express.static(__dirname + '/'));//靜態(tài)目錄
const check = {} //聲明一個對象緩存郵箱和驗證碼酌毡,留著
app.post('/email', function(req, res, next) {
console.log(req.body)
const mail = req.body.email
const data = {
rst: true,
data: "",
msg: ""
}
if (!mail) {
return res.send('參數(shù)錯誤')
} //email出錯時或者為空時
const code = parseInt(Math.random(0, 1) * 10000) //生成隨機驗證碼
check[mail] = code
//發(fā)送郵件
email.sendMail(mail, code, (state) => {
if (state) {
return res.send("發(fā)送成功")
} else {
return res.send("失敗")
}
})
});
/* GET home page. */
app.listen(3001, () => {
console.log('\x1B[33m%s\x1b[0m', "服務開啟成功"); //yellow
})