node發(fā)送郵件非常簡(jiǎn)單,這里只做qq的演示,你可以舉一反三.
使用nodemailer包
let transporter = nodemailer.createTransport({ // 使用qq發(fā)送郵件 // 更多請(qǐng)查看支持列表: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)碼請(qǐng)看:https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html pass: 'xxxxxxxx', }});接下來(lái)我們?cè)O(shè)置我們到發(fā)送內(nèi)容
let mailOpt= { from: '"test" <xxxxxx@qq.com>', // 你到qq郵箱地址 to: 'xxxx@qq.com', // 接受人,可以群發(fā)填寫(xiě)多個(gè)逗號(hào)分隔 subject: 'Hello', // 主題名(郵件名) // 可以發(fā)送text或者h(yuǎn)tml格式,2選1 // text: 'Hello world?', // 純文本 html: '<b>Hello world?</b>' // html};如果我們想發(fā)一個(gè)稍微漂亮到郵件怎么辦?
我們可以使用html模板來(lái)實(shí)現(xiàn)
const template = require('art-template');let html = template(__dirname + '/mail_temp.html', obj) // mail_temp.html為你想使用到頁(yè)面模板,obj為你的參數(shù)// 例如obj = { name : 'test', phone : '183xxxxxxxx', time : new Date()}<section> 新用戶(hù):{{name}}({{phone}})于{{time}}進(jìn)行了注冊(cè).</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/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注