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賬號申請</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;">
注意:此操作可能會修改您的密碼、登錄郵箱或綁定手機(jī)。如非本人操作,請及時登錄并修改密碼以保證帳戶安全
<br>(工作人員不會向你索取此驗(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>
請保管好您的郵箱,避免賬號被他人盜用
</p>
<p>Bertil Chan</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</body>
`
}
sendEmail(data)
運(yùn)行
執(zhí)行命令node nodemailer.js 運(yùn)行起來,這時候就可以在接收者郵箱看到所發(fā)送的郵件了!
擴(kuò)展:node執(zhí)行定時任務(wù)
如果需要實(shí)現(xiàn)定時發(fā)送郵件,可以使用node-schedule這個第三方庫來完成
- 安裝依賴
npm install node-schedule --save
- 修改
nodemailer.js文件中的代碼
// 定時發(fā)送郵件
const nodemailer = require('nodemailer'); //發(fā)送郵件的node插件
const schedule = require('node-schedule'); //執(zhí)行定時任務(wù)的插件
function sendEmail (data){
//這里的內(nèi)容同上
}
schedule.scheduleJob('10 * * * * *', ()=>{
sendEmail(data)
});
- 執(zhí)行命令
node nodemailer.js運(yùn)行,這時每分鐘的第10秒鐘就會自動發(fā)送郵件了。
schedule的6個占位符含義
* * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ └ 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個占位符從左到右分別代表:秒、分、時、日、月、周幾
'*'表示通配符,匹配任意,當(dāng)秒是'*'時,表示任意秒數(shù)都觸發(fā),其它類推
下面可以看看以下傳入?yún)?shù)分別代表的意思
每分鐘的第30秒觸發(fā): '30 * * * * *'
每小時的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)自動發(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)文章
nodejs語言實(shí)現(xiàn)驗(yàn)證碼生成功能的示例代碼
這篇文章主要介紹了nodejs語言實(shí)現(xiàn)驗(yàn)證碼生成功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
使用Node.js自動生成帶動態(tài)圖表的Word文檔
在現(xiàn)代軟件開發(fā)中,動態(tài)生成?Word?文檔是一項(xiàng)非常常見的需求,本文將結(jié)合Node.js和ECharts實(shí)現(xiàn)自動生成帶動態(tài)圖表的Word文檔,感興趣的可以了解下2024-03-03
Node.js開發(fā)教程之基于OnceIO框架實(shí)現(xiàn)文件上傳和驗(yàn)證功能
這篇文章主要介紹了Node.js開發(fā)教程之基于OnceIO框架實(shí)現(xiàn)文件上傳和驗(yàn)證的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11
node.js中的buffer.Buffer.byteLength方法使用說明
這篇文章主要介紹了node.js中的buffer.Buffer.byteLength方法使用說明,本文介紹了buffer.Buffer.byteLength的方法說明、語法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
Ubuntu 16.04 64位中搭建Node.js開發(fā)環(huán)境教程
如果想要在Ubuntu 16.04上安裝Node.js的話,這篇文章對你來說肯定很重要。Node.js從本質(zhì)上來說就是一個運(yùn)行在服務(wù)端上的封裝好了輸入輸出流的javascript程序。本文給大家詳細(xì)介紹了在Ubuntu 16.04 64位搭建Node.js開發(fā)環(huán)境的步驟,有需要的朋友們可以參考學(xué)習(xí)。2016-10-10
node?puppeteer爬蟲爬取電影網(wǎng)站及生成pdf文檔示例
這篇文章主要介紹了node?puppeteer爬蟲爬取電影網(wǎng)站及生成pdf文檔使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
NodeJs項(xiàng)目中關(guān)閉ESLint的方法
ESLint是一個用來識別 ECMAScript 并且按照規(guī)則給出報(bào)告的代碼檢測工具,使用它可以避免低級錯誤和統(tǒng)一代碼的風(fēng)格。這篇文章主要介紹了NodeJs項(xiàng)目中關(guān)閉ESLint的方法,需要的朋友可以參考下2018-08-08

