Node.js使用第三方插件nodemailer实现邮件发送示例

 

环境搭建

npm init -y
npm install nodemailer --save

 

新建文件nodemailer.js

// 邮箱验证
const nodemailer = require('nodemailer'); //发送邮件的node插件
function sendEmail (data){
  let transporter = nodemailer.createTransport({
      host: 'smtp.163.com', 
      port: 465, // SMTP 端口
      auth: {   //发送者的账户和授权码
          user: 'xxx@163.com', //账户
          pass: 'xxx', //smtp授权码,到邮箱设置下获取
      }
  });
  let mailOptions = {
      from: '"Bertil Chan" <xxx@163.com>', // 发送者昵称和地址
      to: data.email, // 接收者的邮箱地址
      subject: '激活验证码', // 邮件主题
      html: data.content
  };
  //发送邮件
  transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
          return console.log(error);
      }
      console.log('邮件发送成功 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;">
                          您正在进行<span style="color: red">XXX账号申请</span>操作,请在验证码输入框中输入:<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;">
                              注意:此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作,请及时登录并修改密码以保证帐户安全
                              <br>(工作人员不会向你索取此验证码,请勿泄漏!)
                          </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>此为系统邮件,请勿回复<br>
                          请保管好您的邮箱,避免账号被他人盗用
                      </p>
                      <p>Bertil Chan</p>
                  </div>
              </div>
          </td>
      </tr>
      </tbody>
  </table>
  </body>
`
}
sendEmail(data)

 

运行

执行命令node nodemailer.js 运行起来,这时候就可以在接收者邮箱看到所发送的邮件了!

扩展:node执行定时任务

如果需要实现定时发送邮件,可以使用node-schedule这个第三方库来完成

  • 安装依赖
npm install node-schedule --save
  • 修改nodemailer.js 文件中的代码
// 定时发送邮件
const nodemailer = require('nodemailer'); //发送邮件的node插件
const schedule = require('node-schedule'); //执行定时任务的插件
function sendEmail (data){
  //这里的内容同上
}
schedule.scheduleJob('10 * * * * *', ()=>{
  sendEmail(data)
});
  • 执行命令node nodemailer.js 运行,这时每分钟的第10秒钟就会自动发送邮件了。

 

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个占位符从左到右分别代表:秒、分、时、日、月、周几

'*'表示通配符,匹配任意,当秒是'*'时,表示任意秒数都触发,其它类推

下面可以看看以下传入参数分别代表的意思

每分钟的第30秒触发: '30 * * * * *'
每小时的1分30秒触发 :'30 1 * * * *'
每天的凌晨1点1分30秒触发 :'30 1 1 * * *'
每月的1日1点1分30秒触发 :'30 1 1 1 * *'
2016年的1月1日1点1分30秒触发 :'30 1 1 1 2016 *'
每周1的1点1分30秒触发 :'30 1 1 * * 1'

以上就是Node.js使用第三方插件nodemailer实现邮件发送示例的详细内容,更多关于Node.js nodemailer发送邮件的资料请关注编程宝库其它相关文章!

 为什页面刷新会出现404因为vue项目中路由hash模式改为了history模式,由于hash模式时url带的#号后面是哈希值不会作为url的一部分发送给 ...