node發送郵件非常簡單,這里只做qq的演示,你可以舉一反三.
使用nodemailer包
let transporter = nodemailer.createTransport({ // 使用qq發送郵件 // 更多請查看支持列表:https://nodemailer.com/smtp/well-known/ service: 'qq', port: 465, // SMTP 端口 secureConnection: true, // 使用了 SSL auth: { user: '751734566@qq.com', // 這里密碼不是qq密碼,是你設置的smtp授權碼 // 獲取qq授權碼請看:https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html pass: 'xxxxxxxx', }});
接下來我們設置我們到發送內容
let mailOpt= { from: '"test" <xxxxxx@qq.com>', // 你到qq郵箱地址 to: 'xxxx@qq.com', // 接受人,可以群發填寫多個逗號分隔 subject: 'Hello', // 主題名(郵件名) // 可以發送text或者html格式,2選1 // text: 'Hello world?', // 純文本 html: '<b>Hello world?</b>' // html};
如果我們想發一個稍微漂亮到郵件怎么辦?
我們可以使用html模板來實現
const template = require('art-template');let html = template(__dirname + '/mail_temp.html', obj) // mail_temp.html為你想使用到頁面模板,obj為你的參數// 例如obj = { name : 'test', phone : '183xxxxxxxx', time : new Date()}<section> 新用戶:{{name}}({{phone}})于{{time}}進行了注冊.</section>所有準備完成,讓我們發送郵件吧!// 執行發送transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('郵件已發送成功,郵件id: %s', info.messageId);});
文檔參考
nodemailer : https://www.npmjs.com/package/nodemailer
art-template : https://aui.github.io/art-template/zh-cn/docs/
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答