簡單兩步使用node傳送qq郵件
node傳送郵件非常簡單,這裡只做qq的演示,你可以舉一反三.
使用nodemailer包
let transporter = nodemailer.createTransport({ // 使用qq傳送郵件 // 更多請檢視支援列表:https://nodemailer.com/smtp/well-known/ service: 'qq', port: 465, // SMTP 埠 secureConnection: true, // 使用了 SSL auth: { user: '[email protected]', // 這裡密碼不是qq密碼,是你設定的smtp授權碼 // 獲取qq授權碼請看:https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html pass: 'xxxxxxxx', } });
接下來我們設定我們到傳送內容
let mailOpt= { from: '"test" <[email protected]>',// 你到qq郵箱地址 to: '[email protected]', // 接受人,可以群發填寫多個逗號分隔 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...
art-template :https://aui.github.io/art-tem...