使用node发送邮箱邮件

发布时间:2019-06-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用node发送邮箱邮件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

使用node.js配合QQ域名邮箱完成邮件发送

配置域名邮箱

具体请到QQ邮箱中查看,在账户这一项中查看

开启SMTP服务获取密码

安装node的email模块

install

npm install nodemailer nodemailer-smtp-transport --save

引入模块

const nodemailer = require('nodemailer');

const smtpTransport = require('nodemailer-smtp-transport');

具体实现代码

// 引入email 模块
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');


// 开启一个 SMTP 连接池
var transport = nodemailer.createTransport(smtpTransport({
  host: "smtp.qq.com", // qq邮箱主机
  secure: true, // 使用 SSL
  secureConnection: true, // 使用 SSL
  port: 465, // SMTP 端口
  auth: {
    user: "noreply@binlive.cn", // 账号   你自定义的域名邮箱账号
    pass: "olncjtfcim1232312ahj"    // 密码   你自己开启SMPT获取的密码
  }
}));

//  发送邮件路由
router.post('/api/email', function(req, res){
  var query = req.body.email;

  // 设置邮件内容  可以拼接html 美化发送内容
  let htmlcon= '<div style="background:-webkit-linear-gradient(-45deg,  #5edac1 0%,#327dda 100%,#1a7a93 100%);width:100%;height:500px;" >'+
    '<div style="width: 200px;height:200px;background:url('https://avatars5.githubusercontent.com/u/22450881?v=4') no-repeat; background-size:contain;background-color:#fff; margin:0 auto;position:relative;top:50px; border-radius:5px; box-shadow:2px 2px 2px rgba(1,138,110,.3)">'+
    '<P style="color:#fff;font-weight:900;text-align:center;position:absolute;bottom:-100px;width: 100%;font-Size: 36px;text-shadow:3px 2px 2px rgba(1,138,110,.6)">BinLive</P>'+
    '<a href="http://www.m.binlive.cn/regsuccess?'+""+query+""+'" target="_blank" style="background:-webkit-linear-gradient(-45deg,  #4cb1dd 0%,#4bb2dd 100%,#1a7a93 100%);display:block;padding:10px;text-decoration:none;color:#fff;text-align:center;letter-spacing:4px;border-radius:5px;box-shadow:0px 2px 2px 1px rgba(1,138,110,.15);box-sizing:border-box; position:absolute;bottom:-150px;width:200px;">完成注册</a>'+
    '</div>'+
    '</div>';

  var mailOptions = {
    from: "noreply@binlive.cn", // 发件地址
    to: req.body.email, // 收件列表
    subject: "BinLive账号注册", // 标题
    text:"",
    html: htmlcon // html 内容
  }
  // 发送邮件
  var userobj={
    email:req.body.email,
    passworld:req.body.passworld,
    hash:hash,
    isregister:false
  }
  transport.sendMail(mailOptions, function(error, response) {
    if(error){
      console.log("fail: " + error);
      console.log("发送失败");
      res.json({data:1})
    }else{
      console.log("发送成功");
      res.json({data:0})
    }
    transport.close(); // 如果没用,关闭连接池
  });
})

脚本宝典总结

以上是脚本宝典为你收集整理的使用node发送邮箱邮件全部内容,希望文章能够帮你解决使用node发送邮箱邮件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。