Vue + Nodejs 发送邮件

一、参考

Nodemailer :: Nodemailer

Vue.js 使用Vue.js如何发送邮件|极客教程 (geek-docs.com)

Node.js使用Nodemailer发送邮件 - JavaScript之禅 - SegmentFault 思否

前端使用nodemailer发送邮件的过程及踩坑及问题解决_vue 无法安装nodemailer-CSDN博客

完美解决 node.js 模块化后报错 require is not defined_node require is not defined-CSDN博客

vue前端+nodejs后端通讯最简单demo_node vue前后端同个端口-CSDN博客

二、获取授权码

开启 POP3/SMTP服务(以QQ邮箱为例):

进入邮箱的设置页面,点击生成授权码

根据要求发送指定短信

验证完毕后即可获取授权码

三、相关代码

3.1 Vue 设置

安装axios: npm i axios ,案例中指定6666端口为邮件接收端口,可自行替换

xml 复制代码
<script lang="ts" setup>
import axios from 'axios'

// 邮件信息
let obj = {
    from:"2235809286@qq.com",
    to:"2292600704@qq.com",  
    subject:"Hello ✔",
    text:"Hello world?",
    html:"<b>Hello world?</b>"
}

// 发送邮件
const sendEmail = ()=>{
  axios.post("http://127.0.0.1:6666/email",obj).then(res=>{
    console.log(res)
  })
}
</script>

<template>
<button @click="sendEmail">Send Email</button>
</template>

当用户点击按钮后,开始发送邮件

3.2 nodejs 设置

3.2.1 项目初始化

控制台输入 npm init, 并输入相关信息

3.2.2 安装依赖

npm i body-parser emailjs express nodemailer nodemon

3.2.3 修改package.json

加上一行:"type": "module"

3.2.4 创建index.js

在创建的Node项目文件夹中,创建index.js文件,

本案例中,指定发送人 22358092862292600704 发送邮件

内容如下:

javascript 复制代码
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

var nodemailer          = require('nodemailer')
var express             = require('express');
var app                 = express();
var bodyParse           = require('body-parser')

// 指定监听端口
var port = 6666 
var app = express();

//增加头部信息解决跨域问题
app.all('*', function (req, res, next){
   res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("Access-Control-Allow-Credentials", true);
    res.header("X-Powered-By", ' 3.2.1');
    next();
});

//使用bodyParse解释前端提交数据
app.use(bodyParse.urlencoded({extended:true})) ;
app.use(bodyParse.json());

// 处理根目录的get请求
app.get('/',function(req,res){
}) ;

// 处理/login请求
app.post('/email',function(req,res){
    console.log(req)
    main(req.body).catch(console.error);
    //向前台反馈信息
    res.status(200).send(
        "success"
      ) ;
});

// 监听指定端口
var server=app.listen(port);

console.log("服务器已运行");
console.log("监听网址为:http://127.0.0.1:/" + port);

const transporter = nodemailer.createTransport({
  host: "smtp.qq.com",
  port: 587,
  secure: false, // Use `true` for port 465, `false` for all other ports
  auth: {
    user: "2235809286@qq.com",    // 输入发送者的邮件
    pass: "xxxxxxxx",             // 输入生成的授权码
  },
});

// async..await is not allowed in global scope, must use a wrapper
async function main(request) {
  // send mail with defined transport object
  const info = await transporter.sendMail({
    from: request.from, // sender address
    to: request.to, // list of receivers
    subject: request.subject, // Subject line
    text: request.text, // plain text body
    html: request.html, // html body
  });

  console.log("Message sent: %s", info.messageId);
  // Message sent: <d786aa62-4e0a-070a-47ed-0b0666549519@ethereal.email>
}

四、测试

4.1 启动 vue 服务

控制台输入 npm run dev,并进入项目页面

4.2 启动 nodejs 服务

控制台输入 node index.js

4.3 效果演示

相关推荐
诗书画唱1 分钟前
【前端面试题】JavaScript 核心知识点解析(第二十二题到第六十一题)
开发语言·前端·javascript
excel8 分钟前
前端必备:从能力检测到 UA-CH,浏览器客户端检测的完整指南
前端
前端小巷子14 分钟前
Vue 3全面提速剖析
前端·vue.js·面试
悟空聊架构21 分钟前
我的网站被攻击了,被干掉了 120G 流量,还在持续攻击中...
java·前端·架构
CodeSheep22 分钟前
国内 IT 公司时薪排行榜。
前端·后端·程序员
尖椒土豆sss26 分钟前
踩坑vue项目中使用 iframe 嵌套子系统无法登录,不报错问题!
前端·vue.js
遗悲风27 分钟前
html二次作业
前端·html
江城开朗的豌豆30 分钟前
React输入框优化:如何精准获取用户输入完成后的最终值?
前端·javascript·全栈
CF14年老兵31 分钟前
从卡顿到飞驰:我是如何用WebAssembly引爆React性能的
前端·react.js·trae
画月的亮34 分钟前
前端处理导出PDF。Vue导出pdf
前端·vue.js·pdf