koa邮件服务

前言

nodemailer看起来不错。
我们在项目中安装即可 npm install nodemailer

使用

创建一个工具 src\utils\email-helper.ts

import nodemailer from "nodemailer";

// 创建一个transporter 
const transporter = nodemailer.createTransport({
    host: "smtp.qq.com",
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {
        user: "960423114@qq.com",
        pass: "xxxx", //到qq邮箱后台去获取
    },
});

// 封装邮件发送
export const sendMail = (to, content) => {
    const params = {
        from: '"丁丁文档📕" <960423114@qq.com>',
        to, // "bar@example.com, baz@example.com" 或者 "bar@example.com"
        subject: "登录验证码",
        text: "验证码", // plain‑text body
        html: `您的验证码为:<b>${content}</b>`, // HTML body
    }
    return transporter.sendMail(params);
}

使用这个工具

import { sendMail } from '../utils/email-helper';

await sendMail('xxxxx@qq.com', '570329');

效果

400

posted @ 2025-07-16 02:14  丁少华  阅读(9)  评论(0)    收藏  举报