So, I need to make an endpoint in REST which will send Email List, Email Subject & Email Body (in HTML).

Then I need to send the emails to all of the specified.

The code that i wrote is working fine. But I need to get the Success or Error for each mail sent.

Since I know Node is asynchronous, we have to use promise.

Thus I used nodemailer-promise to do this. even this is working fine, but I still can't get the infos for each mail sent.

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;

The success_list and failuer_list is always returned empty.

Further, I don't know if my implementation of recursive function is correct, I frankly have little experience in NodeJS. So, if I am implemented it wrong, please guide me.

1 Answers

0
Siddhant Ghosh On Best Solutions

So, I figured out a way to achieve what I was trying using Promise.

The following code is responsible for the output I wanted.

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;

The above code gives the output as

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

This is the solution I wanted. I might still need to refine the response, but it is fairly what I need.