欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Node.js使用第三方插件nodemailer實(shí)現(xiàn)郵件發(fā)送示例

 更新時(shí)間:2022年11月23日 14:39:47   作者:Bertil  
這篇文章主要為大家介紹了Node.js使用第三方插件nodemailer實(shí)現(xiàn)郵件發(fā)送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

環(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)文章!

相關(guān)文章

  • nodejs npm包管理的配置方法及常用命令介紹

    nodejs npm包管理的配置方法及常用命令介紹

    這篇文章主要介紹了nodejs npm包管理的配置方法及常用命令介紹,需要的朋友可以參考下
    2014-06-06
  • 在Node.js中實(shí)現(xiàn)后端與前端的交互的方法詳解

    在Node.js中實(shí)現(xiàn)后端與前端的交互的方法詳解

    在前后端不分離的應(yīng)用模式中,前端頁面看到的效果都是由后端控制,由后端渲染頁面或重定向,也就是后端需要控制前端的展示,前端與后端的耦合度很高, 所以本文給大家介紹了在Node.js中實(shí)現(xiàn)后端與前端的交互的方法,需要的朋友可以參考下
    2024-09-09
  • 詳解Node 定時(shí)器

    詳解Node 定時(shí)器

    這篇文章主要介紹了Node 定時(shí)器的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • nodejs+express實(shí)現(xiàn)文件上傳下載管理網(wǎng)站

    nodejs+express實(shí)現(xiàn)文件上傳下載管理網(wǎng)站

    這篇文章主要為大家詳細(xì)介紹了nodejs+express實(shí)現(xiàn)文件上傳下載管理的網(wǎng)站,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • nodejs實(shí)現(xiàn)獲取某寶商品分類

    nodejs實(shí)現(xiàn)獲取某寶商品分類

    這篇文章主要介紹了nodejs實(shí)現(xiàn)獲取某寶商品分類,十分的簡單實(shí)用,進(jìn)入后臺(tái)直接打開控制臺(tái),把代碼粘進(jìn)去運(yùn)行就OK了,有需要的小伙伴可以參考下。
    2015-05-05
  • 教你如何用node連接redis的示例代碼

    教你如何用node連接redis的示例代碼

    這篇文章主要介紹了教你如何用node連接redis的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • node.js 核心http模塊,起一個(gè)服務(wù)器,返回一個(gè)頁面的實(shí)例

    node.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實(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版本控制的實(shí)現(xiàn)

    NVM是Node.js的版本管理工具,可以方便地在不同版本的Node.js之間切換,本文主要介紹了簡單易懂的nvm和Node.js版本控制的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • 一文詳解nodejs的path模塊使用

    一文詳解nodejs的path模塊使用

    這篇文章主要為大家介紹了nodejs的path模塊使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評論