使用 Promise、Nodemailer / Nodemailer-promise 和 Expressjs 从电子邮件列表发送电子邮件

Sending emails from a list of email using Promise, Nodemailer / Nodemailer-promise with Expressjs

所以,我需要在 REST 中创建一个端点,它将发送 电子邮件列表、电子邮件主题和电子邮件正文(HTML)。

然后我需要将邮件发送给所有指定的人。

我写的代码运行良好。 但是我需要为每封发送的邮件获取成功或错误。

既然知道Node是异步的,就得用promise。

因此我使用了 nodemailer-promise 来做到这一点。即使这样工作正常,但我仍然无法获取每封已发送邮件的信息

const express = require('express');
const promise_database = require('../common/promise-database');
const utils = require('../common/utils');

var router = express.Router();

router.post(`/send-emails`, (request, response) => {
    const subject = request.body.subject;
    const email_body = request.body.email_body;
    const email_ids = request.body.email_ids;
    (send_emails(email_ids, subject, email_body, response));
});

function send_emails(email_ids, subject, email_body, response) {
    send_messages(email_ids, subject, email_body, 0, response);
}

function send_messages(email_ids, subject, email_body, count, response)  {
    success_list = [];
    failuer_list = [];
    response = response;
    console.log(response);
    const transporter = utils.createPromiseMailer();
        var mailOptions = {
            from: 'Royal Fitness Club',
            to: email_ids[count],
            subject: subject,
            html: email_body
        };
        // send mail with defined transport object
        transporter(mailOptions).then(info => {
            success_list.push(info);
            send_messages(email_ids, subject, email_body, count + 1, response);
        }).catch( error => {
            failuer_list.push(error);
        }).then(() => {
            response.send({"success_list": success_list, "failuer_list": failuer_list});
        });
}

module.exports = router;

success_listfailuer_list总是返回.

另外,我不知道我递归函数的实现是否正确,坦白说我对NodeJS的经验很少。 所以,如果我实现的不对,请指导我。

所以,我想出了一种方法来实现我正在尝试使用 Promise 的东西。

下面的代码负责我想要的输出。

const express = require('express');
const promise_database = require('../common/promise-database');
const utils = require('../common/utils');

var router = express.Router();

router.post(`/send-emails`, (request, response) => {
    const subject = request.body.subject;
    const email_body = request.body.email_body;
    const email_ids = request.body.email_ids;
    success_list = [];
    failure_list = [];
    send_messages(email_ids, subject, email_body, 0, success_list, failure_list)
    .then(info => {
        response.send({"success_list": success_list,"failure_list": failure_list});
    }).catch( error => {
        response.send({"success_list": success_list,"failure_list": failure_list});
    });
});

function send_messages(email_ids, subejct, email_body, count, success_list, failure_list) {
    success_list = success_list;
    failure_list = failure_list;
    const transporter = utils.createPromiseMailer();
    var mailOptions = {
        from: 'Royal Fitness Club',
        to: email_ids[count],
        subject: subejct,
        html: email_body
    };
    // send mail with defined transport object
    return new Promise((resolve, reject) => {
        transporter(mailOptions).then(info => {
            if (count < Object.keys(email_ids).length - 1) {
                resolve(send_messages(email_ids, subejct, email_body, count + 1, success_list, failure_list));
                success_list.push(info);
            } else {
                success_list.push(info);
                resolve();
            }
        }).catch(error => {
            failure_list.push(error);
            reject(failure_list);
        }); 
    });
}

module.exports = router;

以上代码给出的输出为

{
    "success_list": [
        {
            "accepted": [
                "XXXXXXXXXXX@gmail.com"
            ],
            "rejected": [],
            "envelopeTime": 936,
            "messageTime": 983,
            "messageSize": 256,
            "response": "250 2.0.0 OK  1557735125 79sm9995956pfz.144 - gsmtp",
            "envelope": {
                "from": "",
                "to": [
                    "XXXXXXXXXXX@gmail.com"
                ]
            },
            "messageId": "<XXXXXXXXXXX>"
        },
        {
            "accepted": [
                "XXXXXXXXXXX@gmail.com"
            ],
            "rejected": [],
            "envelopeTime": 849,
            "messageTime": 894,
            "messageSize": 255,
            "response": "250 2.0.0 OK  1557735128 a80sm34147685pfj.105 - gsmtp",
            "envelope": {
                "from": "",
                "to": [
                    "XXXXXXXXXXX@gmail.com"
                ]
            },
            "messageId": "<XXXXXXXXXXX>"
        }
    ],
    "failure_list": []
}

这就是我想要的解决方案。 我可能仍需要完善响应,但这正是我所需要的。