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ā)送者的賬戶(hù)和授權(quán)碼
user: 'xxx@163.com', //賬戶(hù)
pass: 'xxx', //smtp授權(quán)碼,到郵箱設(shè)置下獲取
}
});
let mailOptions = {
from: '"Bertil Chan" <xxx@163.com>', // 發(fā)送者昵稱(chēng)和地址
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;">尊敬的用戶(hù):<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)申請(qǐng)</span>操作,請(qǐng)?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ī)。如非本人操作,請(qǐng)及時(shí)登錄并修改密碼以保證帳戶(hù)安全
<br>(工作人員不會(huì)向你索取此驗(yàn)證碼,請(qǐng)勿泄漏!)
</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)郵件,請(qǐng)勿回復(fù)<br>
請(qǐng)保管好您的郵箱,避免賬號(hào)被他人盜用
</p>
<p>Bertil Chan</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</body>
`
}
sendEmail(data)
運(yùn)行
執(zhí)行命令node nodemailer.js 運(yùn)行起來(lái),這時(shí)候就可以在接收者郵箱看到所發(fā)送的郵件了!
擴(kuò)展:node執(zhí)行定時(shí)任務(wù)
如果需要實(shí)現(xiàn)定時(shí)發(fā)送郵件,可以使用node-schedule這個(gè)第三方庫(kù)來(lái)完成
- 安裝依賴(lài)
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ā),其它類(lèi)推
下面可以看看以下傳入?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ā)送郵件的資料請(qǐng)關(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請(qǐng)求功能(發(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)用模式中,前端頁(yè)面看到的效果都是由后端控制,由后端渲染頁(yè)面或重定向,也就是后端需要控制前端的展示,前端與后端的耦合度很高, 所以本文給大家介紹了在Node.js中實(shí)現(xiàn)后端與前端的交互的方法,需要的朋友可以參考下2024-09-09
nodejs+express實(shí)現(xiàn)文件上傳下載管理網(wǎng)站
這篇文章主要為大家詳細(xì)介紹了nodejs+express實(shí)現(xiàn)文件上傳下載管理的網(wǎng)站,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
nodejs實(shí)現(xiàn)獲取某寶商品分類(lèi)
這篇文章主要介紹了nodejs實(shí)現(xiàn)獲取某寶商品分類(lèi),十分的簡(jiǎn)單實(shí)用,進(jìn)入后臺(tái)直接打開(kāi)控制臺(tái),把代碼粘進(jìn)去運(yùn)行就OK了,有需要的小伙伴可以參考下。2015-05-05
node.js 核心http模塊,起一個(gè)服務(wù)器,返回一個(gè)頁(yè)面的實(shí)例
下面小編就為大家?guī)?lái)一篇node.js 核心http模塊,起一個(gè)服務(wù)器,返回一個(gè)頁(yè)面的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
基于Node實(shí)現(xiàn)可以操作MySQL的接口
這篇文章主要介紹了用Node寫(xiě)個(gè)可以操作MySQL的接口,以前也用Node寫(xiě)過(guò)接口,但不涉及數(shù)據(jù)庫(kù)操作,而我們發(fā)現(xiàn),后端寫(xiě)接口,基本都繞不開(kāi)數(shù)據(jù)庫(kù)操作,感覺(jué)不寫(xiě)一個(gè)能操作數(shù)據(jù)庫(kù)的接口,就不算真正意義上學(xué)會(huì)了寫(xiě)接口,那我們今天就學(xué)習(xí)一下,如何寫(xiě)一個(gè)可以操作數(shù)據(jù)庫(kù)的接口2024-05-05
簡(jiǎn)單易懂的nvm和Node.js版本控制的實(shí)現(xiàn)
NVM是Node.js的版本管理工具,可以方便地在不同版本的Node.js之間切換,本文主要介紹了簡(jiǎn)單易懂的nvm和Node.js版本控制的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10

