创建一个快速中间件来发送电子邮件

发布于 2025-01-11 02:18:24 字数 265 浏览 0 评论 0原文

我一直在尝试制作一个快速中间件,在前一个中间件完成后使用 Nodemailer 发送电子邮件。我提出了几种不同的设计,但最终每个不同的版本都有其缺点。

最终,我希望中间件能够得到前一个中间件的响应。如果成功,则发送成功邮件,否则发送错误邮件。

我提出了一种双重设计,其中一个变体会推送到一个错误中间件,而成功则会推送到下一个中​​间件。这包含发送多个标头的一些小问题,特别是在第二个中间件错误上。我可以说,如果邮件出错,什么也不做。但这似乎不对。如果有人对好的设计有任何建议,那就太好了。

I've been trying to make an express middleware that sends an email using Nodemailer after the previous middleware finishes. I've come up with a few different designs, but ultimately each different version has it's drawback.

Ultimately, I would like the middleware to have a response from the previous middleware. If it is a success, then send a success email, otherwise, send an error email.

I came up with a dual design where one variation pushes to an error middleware, and a success leads to the next middleware. This contains some slight issues of sending multiple headers, specifically on an the second middleware erroring. I could say, if the mail errors out, do nothing. But that doesn't seem right. If anyone has any suggestions on a good design, that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2025-01-18 02:18:24

根据您的描述,我建议不要为此创建不同的中间件,而只是创建一个可以处理不同类型消息的通用电子邮件函数。然后,只需在第一个中间件中使用该函数并根据用例(成功/错误)传递不同的参数。

email-controller.js

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  host: process.env.EMAIL_HOST,
  port: process.env.EMAIL_PORT,
  secure: true,
  auth: {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASSWORD,
  },
});

exports.send_email_message = (send_to, subject, message) => {

  return new Promise((resolve, reject) => {

    const email_message = {
      from: { name: process.env.EMAIL_FRIENDLY_NAME },
      to: send_to,
      subject: subject,
      text: message
    };

    transporter.sendMail(email_message).then(() => {
      resolve(true);
    }).catch((error) => {
      reject(false);
    });

  })

}

自定义-router.js

const { send_email_message } = require('./email-controller');
const express = require('express');
const router = express.Router();

router.get('/custom-middleware', async (req, res, next) => {
  try {
    // You can calculate "success" variable based on your custom logic
    if(success){
      await send_email_message('[email protected]', 'Success', 'This is body of success message.');
      return res.status(200).json({ success: true });
    } else {
      await send_email_message('[email protected]', 'Error', 'This is body of error message.');
      return res.status(400).json({ success: false });
    }
  } catch(error) {
    return res.status(400).json({ success: false });
  }
});

module.exports = router;

From what you described, I would suggest not to create different middleware for that, but to just create one generic email function that would handle different type of messages. Then, just use that function in the first middleware and pass different parameters based on use case (success/error).

email-controller.js

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  host: process.env.EMAIL_HOST,
  port: process.env.EMAIL_PORT,
  secure: true,
  auth: {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASSWORD,
  },
});

exports.send_email_message = (send_to, subject, message) => {

  return new Promise((resolve, reject) => {

    const email_message = {
      from: { name: process.env.EMAIL_FRIENDLY_NAME },
      to: send_to,
      subject: subject,
      text: message
    };

    transporter.sendMail(email_message).then(() => {
      resolve(true);
    }).catch((error) => {
      reject(false);
    });

  })

}

custom-router.js

const { send_email_message } = require('./email-controller');
const express = require('express');
const router = express.Router();

router.get('/custom-middleware', async (req, res, next) => {
  try {
    // You can calculate "success" variable based on your custom logic
    if(success){
      await send_email_message('[email protected]', 'Success', 'This is body of success message.');
      return res.status(200).json({ success: true });
    } else {
      await send_email_message('[email protected]', 'Error', 'This is body of error message.');
      return res.status(400).json({ success: false });
    }
  } catch(error) {
    return res.status(400).json({ success: false });
  }
});

module.exports = router;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文