前端使用nodemailer发送邮件的过程及踩坑及问题解决

1、版本及问题

我在使用nodejs和Vue构建前端界面的时候,希望在一个界面中直接实现一个发送邮件的功能。因此,当时希望减免麻烦直接在Vue项目中引入了nodemailer的包,然后就出现了一系列的问题,本文描述相这些问题,并给出自己的解决方案。首先使用npm install nodemailer和 npm install dns命令安装这两个库,。注意:dns是必备的,要启动服务时会报错告诉你缺少dns这个库。在版本介绍后,会介绍授权码的获取过程(以qq为例)。

nodemailer 0.7.1版本

在开始使用nodemailer这个攻击的时候,首先会在网上搜索用法,极有可能会查到如下代码:

var smtpTransport = nodemailer.createTransport("SMTP",{
  host: "smtp.qq.com", //连接的邮箱服务器
  secureConnection: true, // 使用 SSL进行加密
  port: 465, // SMTP的端口号
  auth: {
    user: "xxxxxxxx@qq.com", // 你的邮箱账号,这里以qq邮箱为例
    pass: "xxxxxxxx" // 授权码,在你开启邮箱的的pos/smtp服务时能够得到的授权码,不是邮箱密码
  }
});

// 设置邮件内容
var mailOptions = {
  from: "Fred Foo <xxxxxxxx@qq.com>", // 发件地址,就是上面你输入的邮箱
  to: "2838890xx@qq.com, minimixx@126.com", // 收件列表,要发送的目标邮箱
  subject: "Hello world", // 标题
  html: "<b>thanks a for visiting!</b> 世界,你好!" // html 内容
}

// 发送邮件
smtpTransport.sendMail(mailOptions, function(error, response){
  if(error){
    console.log(error);
  }else{
    console.log("Message sent: " + response.message);
  }
  smtpTransport.close(); 
});

上述代码就是标准的0.7版本的写法,在更高的版本不能使用,因此如果使用的话需要注意安装时一定要安装0.7版本。如npm install nodemailer@0.7.1

nodemailer 2.7.2版本

上述版本已经是很老的一个版本,目前已经出了一些更新得版本,2.7.2也是一个老版本,但是网上依旧有这部分的代码解决方案这是nodemailer 2.7.2的官方网站。代码如下:

//首先是一个你的邮箱地址,然后时是授权码(和0.7.1版本的授权码获取方式是一样的),最后的邮件服务器。
var transporter = nodemailer.createTransport('smtps://xx@gmail.com:pass@smtp.gmail.com');
var mailOptions = {
    from: '"Fred Foo ?" <foo@blurdybloop.com>', // 你的邮箱
    to: 'bar@blurdybloop.com, baz@blurdybloop.com', // 要发送的目的邮箱地址列表
    subject: 'Hello ✔', // 主题
    text: 'Hello world ?', // 内容
    html: '<b>Hello world ?</b>' // html 
};

// 发送邮件
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

nodemailer 6.7.0版本

这是目前最新版本的nodemailer库,现在展现你最有可能搜到的另一端代码,也是我使用的代码,nodemailer的仓库地址代码如下:

const nodemailer = require("nodemailer");
let transporter = nodemailer.createTransport({
    service: 'qq', //使用的邮箱服务,这里qq为例
    port: 465, //邮箱服务端口号
    secure: false, // true for 465, false for other ports
    auth: {
      user: "xxxxxxxx@qq.com", //  邮箱地址
      pass: "************", //授权码
    },
  });

  // send mail with defined transport object
  transporter.sendMail({
    from: 'xxxxxxxx@qq.com', // 你的邮箱
    to: req.query.email, //发送的邮箱列表
    subject: req.query.name, // 主题
    text: req.query.msg, // 文本内容
  }).then(res=>{
    console.log(res)
  });

授权码的获取方式

这里是以qq邮箱为例,首先是登陆qq邮箱,然后点击设置,如下图所示:

图片:![Alt]在这里插入图片描述
然后点击账户,如下图所示: 在这里插入图片描述

然后找到如下图所示的部分:
在这里插入图片描述
这里我第一个POS3/SMTP已经开启了,如果未做过相关工作则会显示 已关闭 。开启第二个也是可以的,这两个开启一个即可。开启后有获得授权码的相关操作,得到授权码后直接复制使用即可。

2.解决方案

我最开始的时候是想将nodemailer 模块引入到Vue中,然后为保证nodemailer 的使用需要下载dns,但是在引入dns库后,Vue就老是提示项目中有‘.’的错误模块。我切换了上述三个版本均不能解决问题,包括在Vue中构架mock和express服务均不行。因此,我最后单独构建了一个nodejs的服务将nodemailer 模块单独作为一个服务开启。用我的前端界面去连接这个服务实现了邮件的成功发送。
我这里使用的服务框架是express模块。
首先使用npm install express命令安装express库,然后使用npm install body-parser安装body-parser库,这个库是用来解析参数的。
具体代码如下:

const nodemailer = require("nodemailer");
var bodyParser = require("body-parser");
let express = require('express');
let app = express();
app.use(bodyParser.urlencoded({extended: false}))// 这一句会解决掉一个错误

app.get('/email', function(req, res) {
  let transporter = nodemailer.createTransport({
    service: 'qq',
    port: 465,
    secure: false, 
    auth: {
      user: "xxxxxxxx@qq.com", 
      pass: "**************",
    },
  });

  // send mail with defined transport object
  transporter.sendMail({
    from: 'xxxxxxxxx@qq.com', 
    to: req.query.email, // 这里req.query获取get请求参数内容,若使用post请求则用req.body获取
    subject: req.query.name, 
    text: req.query.msg,
  }).then(res=>{
    console.log(res)
  });
});
app.listen('8081');//开启服务端口号8081
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以!nodemailer是一个流行的Node.js库,用于发送电子邮件。下面是一个简单的nodemailer发送邮件的教程: 1. 首先,确保你已经安装了Node.js和npm。 2. 在你的项目文件夹中,打开终端并运行以下命令来安装nodemailer: ``` npm install nodemailer ``` 3. 创建一个新的JavaScript文件,比如`sendEmail.js`。 4. 在文件中引入nodemailer模块: ```javascript const nodemailer = require('nodemailer'); ``` 5. 创建一个邮件传输对象,并配置SMTP传输选项: ```javascript let transporter = nodemailer.createTransport({ service: 'Gmail', auth: { user: 'your_email@gmail.com', pass: 'your_password' } }); ``` 请注意,这里使用了Gmail作为邮件服务提供商,你需要替换成你自己的邮箱地址和密码。 6. 创建一个包含邮件内容的选项对象: ```javascript let mailOptions = { from: 'your_email@gmail.com', to: 'recipient_email@example.com', subject: 'Hello from nodemailer', text: 'This is a test email sent using nodemailer.' }; ``` 请将`from`字段替换为你自己的邮箱地址,`to`字段替换为收件人的邮箱地址。 7. 使用`transporter.sendMail()`方法发送邮件: ```javascript transporter.sendMail(mailOptions, function(error, info){ if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } }); ``` 如果邮件发送成功,你将会在控制台看到`Email sent: <message_id>`的输出。 这就是一个简单的nodemailer发送邮件的教程。你可以根据自己的需求进行更多的配置,比如添加附件、HTML内容等。如果你想了解更多关于nodemailer的详细信息,可以查阅官方文档:https://nodemailer.com/about/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凉寒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值