nodejs實現發送郵件之(express篇)

Nodemailer簡介

Nodemailer是一個簡單易用的Node.js郵件發送組件

官網地址:https://nodemailer.com

GitHub地址:https://github.com/nodemailer/nodemailer

Nodemailer的主要特點包括:

  • 支持Unicode編碼
  • 支持Window系統環境
  • 支持HTML內容和普通文本內容
  • 支持附件(傳送大附件)
  • 支持HTML內容中嵌入圖片
  • 支持SSL/STARTTLS安全的郵件發送
  • 支持內置的transport方法和其他插件實現的transport方法
  • 支持自定義插件處理消息
  • 支持XOAUTH2登錄驗證

安裝使用:

1.先下載

npm install nodemailer --save

2.封裝sendEmail.js

這裏我對發送郵件進行了簡單的封裝,用的時候只需要傳參數就好了

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: '[email protected]', // 發送方的郵箱
		pass: 'xxxx' // smtp 的授權碼
	}
});
//pass 不是郵箱賬戶的密碼而是stmp的授權碼(必須是相應郵箱的stmp授權碼)
//郵箱---設置--賬戶--POP3/SMTP服務---開啓---獲取stmp授權碼

function sendMail(mail, code, call) {
	// 發送的配置項
	let mailOptions = {
		from: '"Express-demo" <[email protected]>', // 發送方
		to: mail, //接收者郵箱,多個郵箱用逗號間隔
		subject: '歡迎來到"Express-demo"', // 標題
		text: 'Hello world?', // 文本內容
		html: '<p>這裏是"Express-demo"詳情請點擊:</p><a href="https://www.jianshu.com/u/5cdc0352bf01">點擊跳轉</a>', //頁面內容
		// attachments: [{//發送文件
		// 		filename: 'index.html', //文件名字
		// 		path: './index.html' //文件路徑
		// 	},
		// 	{
		// 		filename: 'sendEmail.js', //文件名字
		// 		content: 'sendEmail.js' //文件路徑
		// 	}
		// ]
	};

	//發送函數
	transporter.sendMail(mailOptions, (error, info) => {
		if (error) {
			call(false)
		} else {
			call(true) //因爲是異步 所有需要回調函數通知成功結果
		}
	});

}

module.exports = {
	sendMail
}

3.使用方法

const express = require('express')
const app = express()
const email = require('./sendEmail.js'); //引入封裝好的函數
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 + '/'));//靜態目錄
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('參數錯誤')
	} //email出錯時或者爲空時
	const code = parseInt(Math.random(0, 1) * 10000) //生成隨機驗證碼
	check[mail] = code
	//發送郵件
	email.sendMail(mail, code, (state) => {
		if (state) {
			return res.send("發送成功")
		} else {
			return res.send("失敗")
		}

	})


});

/* GET home page. */

app.listen(3001, () => {
	console.log('\x1B[33m%s\x1b[0m', "服務開啓成功"); //yellow

})

源碼地址

參考鏈接1

參考鏈接2

官網

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