簡單兩步使用node發(fā)送qq郵件的方法
node發(fā)送郵件非常簡單,這里只做qq的演示,你可以舉一反三.
使用nodemailer包
let transporter = nodemailer.createTransport({
// 使用qq發(fā)送郵件
// 更多請查看支持列表:https://nodemailer.com/smtp/well-known/
service: 'qq',
port: 465, // SMTP 端口
secureConnection: true, // 使用了 SSL
auth: {
user: '751734566@qq.com',
// 這里密碼不是qq密碼,是你設(shè)置的smtp授權(quán)碼
// 獲取qq授權(quán)碼請看:https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html
pass: 'xxxxxxxx',
}
});
接下來我們設(shè)置我們到發(fā)送內(nèi)容
let mailOpt= {
from: '"test" <xxxxxx@qq.com>', // 你到qq郵箱地址
to: 'xxxx@qq.com', // 接受人,可以群發(fā)填寫多個逗號分隔
subject: 'Hello', // 主題名(郵件名)
// 可以發(fā)送text或者html格式,2選1
// text: 'Hello world?', // 純文本
html: '<b>Hello world?</b>' // html
};
如果我們想發(fā)一個稍微漂亮到郵件怎么辦?
我們可以使用html模板來實現(xiàn)
const template = require('art-template');
let html = template(__dirname + '/mail_temp.html', obj) // mail_temp.html為你想使用到頁面模板,obj為你的參數(shù)
// 例如
obj = {
name : 'test',
phone : '183xxxxxxxx',
time : new Date()
}
<section>
新用戶:{{name}}({{phone}})于{{time}}進行了注冊.
</section>
所有準(zhǔn)備完成,讓我們發(fā)送郵件吧!
// 執(zhí)行發(fā)送
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('郵件已發(fā)送成功,郵件id: %s', info.messageId);
});
文檔參考
nodemailer : https://www.npmjs.com/package/nodemailer
art-template : https://aui.github.io/art-template/zh-cn/docs/
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
nodejs環(huán)境快速操作mysql數(shù)據(jù)庫的方法詳解
這篇文章主要介紹了nodejs環(huán)境快速操作mysql數(shù)據(jù)庫的方法詳解,需要的朋友可以參考下2021-03-03
Node.js?中使用fetch?按JSON格式發(fā)post請求的問題解析
最近在測試一個api,可以用curl命令直接訪問,指定header相關(guān)配置,request?body(JSON),成功后返回一個JSON,這篇文章主要介紹了Node.js?中使用fetch?按JSON格式發(fā)post請求,需要的朋友可以參考下2023-04-04
Node.js中的HTTP?Server對象與GET、POST請求
這篇文章介紹了Node.js中的HTTP?Server對象與GET、POST請求,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

