Node.js使用第三方插件nodemailer實(shí)現(xiàn)郵件發(fā)送示例
環(huán)境搭建
npm init -y npm install nodemailer --save
新建文件nodemailer.js
// 郵箱驗(yàn)證 const nodemailer = require('nodemailer'); //發(fā)送郵件的node插件 function sendEmail (data){ let transporter = nodemailer.createTransport({ host: 'smtp.163.com', port: 465, // SMTP 端口 auth: { //發(fā)送者的賬戶和授權(quán)碼 user: 'xxx@163.com', //賬戶 pass: 'xxx', //smtp授權(quán)碼,到郵箱設(shè)置下獲取 } }); let mailOptions = { from: '"Bertil Chan" <xxx@163.com>', // 發(fā)送者昵稱和地址 to: data.email, // 接收者的郵箱地址 subject: '激活驗(yàn)證碼', // 郵件主題 html: data.content }; //發(fā)送郵件 transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('郵件發(fā)送成功 ID:', info.messageId); }); } let yzm = 'dslk' let data = { email:'xxx@qq.com', // 接收者的郵箱 // 郵件模板,可自行修改 content:` <head> <base target="_blank" /> <style type="text/css">::-webkit-scrollbar{ display: none; }</style> <style id="cloudAttachStyle" type="text/css">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style> <style id="blockquoteStyle" type="text/css">blockquote{display:none;}</style> <style type="text/css"> body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px} td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana} pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%} th,td{font-family:arial,verdana,sans-serif;line-height:1.666} img{ border:0} header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block} blockquote{margin-right:0px} </style> </head> <body tabindex="0" role="listitem"> <table width="700" border="0" align="center" cellspacing="0" style="width:700px;"> <tbody> <tr> <td> <div style="width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;"> <table border="0" cellpadding="0" cellspacing="0" width="700" height="39" style="font:12px Tahoma, Arial, 宋體;"> <tbody><tr><td width="210"></td></tr></tbody> </table> </div> <div style="width:680px;padding:0 10px;margin:0 auto;"> <div style="line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;"> <strong style="display:block;margin-bottom:15px;">尊敬的用戶:<span style="color:#f60;font-size: 16px;"></span>您好!</strong> <strong style="display:block;margin-bottom:15px;"> 您正在進(jìn)行<span style="color: red">XXX賬號(hào)申請</span>操作,請?jiān)隍?yàn)證碼輸入框中輸入:<span style="color:#f60;font-size: 24px">${yzm}</span>,以完成操作。 </strong> </div> <div style="margin-bottom:30px;"> <small style="display:block;margin-bottom:20px;font-size:12px;"> <p style="color:#747474;"> 注意:此操作可能會(huì)修改您的密碼、登錄郵箱或綁定手機(jī)。如非本人操作,請及時(shí)登錄并修改密碼以保證帳戶安全 <br>(工作人員不會(huì)向你索取此驗(yàn)證碼,請勿泄漏!) </p> </small> </div> </div> <div style="width:700px;margin:0 auto;"> <div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;"> <p>此為系統(tǒng)郵件,請勿回復(fù)<br> 請保管好您的郵箱,避免賬號(hào)被他人盜用 </p> <p>Bertil Chan</p> </div> </div> </td> </tr> </tbody> </table> </body> ` } sendEmail(data)
運(yùn)行
執(zhí)行命令node nodemailer.js
運(yùn)行起來,這時(shí)候就可以在接收者郵箱看到所發(fā)送的郵件了!
擴(kuò)展:node執(zhí)行定時(shí)任務(wù)
如果需要實(shí)現(xiàn)定時(shí)發(fā)送郵件,可以使用node-schedule
這個(gè)第三方庫來完成
- 安裝依賴
npm install node-schedule --save
- 修改
nodemailer.js
文件中的代碼
// 定時(shí)發(fā)送郵件 const nodemailer = require('nodemailer'); //發(fā)送郵件的node插件 const schedule = require('node-schedule'); //執(zhí)行定時(shí)任務(wù)的插件 function sendEmail (data){ //這里的內(nèi)容同上 } schedule.scheduleJob('10 * * * * *', ()=>{ sendEmail(data) });
- 執(zhí)行命令
node nodemailer.js
運(yùn)行,這時(shí)每分鐘的第10秒鐘就會(huì)自動(dòng)發(fā)送郵件了。
schedule的6個(gè)占位符含義
* * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun) │ │ │ │ └───── month (1 - 12) │ │ │ └────────── day of month (1 - 31) │ │ └─────────────── hour (0 - 23) │ └──────────────────── minute (0 - 59) └───────────────────────── second (0 - 59, OPTIONAL)
6個(gè)占位符從左到右分別代表:秒、分、時(shí)、日、月、周幾
'*'表示通配符,匹配任意,當(dāng)秒是'*'時(shí),表示任意秒數(shù)都觸發(fā),其它類推
下面可以看看以下傳入?yún)?shù)分別代表的意思
每分鐘的第30秒觸發(fā): '30 * * * * *'
每小時(shí)的1分30秒觸發(fā) :'30 1 * * * *'
每天的凌晨1點(diǎn)1分30秒觸發(fā) :'30 1 1 * * *'
每月的1日1點(diǎn)1分30秒觸發(fā) :'30 1 1 1 * *'
2016年的1月1日1點(diǎn)1分30秒觸發(fā) :'30 1 1 1 2016 *'
每周1的1點(diǎn)1分30秒觸發(fā) :'30 1 1 * * 1'
以上就是Node.js使用第三方插件nodemailer實(shí)現(xiàn)郵件發(fā)送示例的詳細(xì)內(nèi)容,更多關(guān)于Node.js nodemailer發(fā)送郵件的資料請關(guān)注腳本之家其它相關(guān)文章!
- nodejs實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼功能
- node.js模擬實(shí)現(xiàn)自動(dòng)發(fā)送郵件驗(yàn)證碼
- 如何利用node實(shí)現(xiàn)發(fā)送QQ郵箱驗(yàn)證碼
- Nodejs 發(fā)送Post請求功能(發(fā)短信驗(yàn)證碼例子)
- Node使用Nodemailer發(fā)送郵件的方法實(shí)現(xiàn)
- nodejs模塊nodemailer基本使用-郵件發(fā)送示例(支持附件)
- Node.js使用NodeMailer發(fā)送郵件實(shí)例代碼
- 基于Node.js實(shí)現(xiàn)nodemailer郵件發(fā)送
- node.js使用nodemailer發(fā)送郵件實(shí)例
- node 使用 nodemailer工具發(fā)送驗(yàn)證碼到郵箱
相關(guān)文章
在Node.js中實(shí)現(xiàn)后端與前端的交互的方法詳解
在前后端不分離的應(yīng)用模式中,前端頁面看到的效果都是由后端控制,由后端渲染頁面或重定向,也就是后端需要控制前端的展示,前端與后端的耦合度很高, 所以本文給大家介紹了在Node.js中實(shí)現(xiàn)后端與前端的交互的方法,需要的朋友可以參考下2024-09-09nodejs+express實(shí)現(xiàn)文件上傳下載管理網(wǎng)站
這篇文章主要為大家詳細(xì)介紹了nodejs+express實(shí)現(xiàn)文件上傳下載管理的網(wǎng)站,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03node.js 核心http模塊,起一個(gè)服務(wù)器,返回一個(gè)頁面的實(shí)例
下面小編就為大家?guī)硪黄猲ode.js 核心http模塊,起一個(gè)服務(wù)器,返回一個(gè)頁面的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09基于Node實(shí)現(xiàn)可以操作MySQL的接口
這篇文章主要介紹了用Node寫個(gè)可以操作MySQL的接口,以前也用Node寫過接口,但不涉及數(shù)據(jù)庫操作,而我們發(fā)現(xiàn),后端寫接口,基本都繞不開數(shù)據(jù)庫操作,感覺不寫一個(gè)能操作數(shù)據(jù)庫的接口,就不算真正意義上學(xué)會(huì)了寫接口,那我們今天就學(xué)習(xí)一下,如何寫一個(gè)可以操作數(shù)據(jù)庫的接口2024-05-05簡單易懂的nvm和Node.js版本控制的實(shí)現(xiàn)
NVM是Node.js的版本管理工具,可以方便地在不同版本的Node.js之間切換,本文主要介紹了簡單易懂的nvm和Node.js版本控制的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10