因此,我需要在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_list
和 failuer_list
始终返回空。
此外,我不知道我的递归函数实现是否正确,坦率地说,我对NodeJS的经验很少。 因此,如果实施不正确,请指导我。
答案 0 :(得分:0)
因此,我想出了一种方法来实现我使用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": []
}
这是我想要的解决方案。 我可能仍需要优化响应,但这正是我所需要的。