nodemailer多个收件人不同的上下文

时间:2016-08-03 14:34:42

标签: javascript html node.js email nodemailer

我正在尝试实现一种向多个收件人发送电子邮件的方法,但我也希望将具有收件人姓名的用户属性传递给html模板。 (我使用NODEMAILER作为nodejs模块)

我在此阶段的代码是这样的:

SellerMatched: function (fromUser, offer, cb) {

        var maillist  = [];
        var users = [];

        for (var i=0; i<fromUser.length; i++)
        {
            maillist[i]=fromUser[i].email;
            users[i] = fromUser[i].Name;
        }


        maillist.toString();
        users.toString();



        mail.sendMail({
            from: 'Website Support <help@domain.com>',
            to: maillist,
            subject: 'Seller Matched',
            template: 'SellerMatch',
            context: {
                user: users,
                username: offer.owner.username,
                OfferName: offer.name,
                category: offer.category
            }
        }, cb);
    }

并且html具有以下属性:它是这样的:

...

Hi {{user}},

A offer is uploaded that matched your preference. 
Offer detail:
Name : {{OfferName}}
By: {{username}}
Category: {{category}} 

....

这项工作正常,电子邮件发送给收件人,但在属性Hi {{user}}中,我只想显示收件人的姓名,因为现在它显示了所有收件人的数组。

由于

2 个答案:

答案 0 :(得分:1)

我在https://hashnode.com/post/nodemailer-multiple-recipients-different-context-ciret279f03hduy539u4tolck中看到

var promises = [];
for (var i = 0; i < fromUser.length; i++) {
    promises.push(new Promise(function(resolve, reject) {
        mail.sendMail({
            from: 'Website Support <help@domain.com>',
            to: fromUser[i].email,
            subject: 'Seller Matched',
            template: 'SellerMatch',
            context: {
                user: fromUser[i].Name,
                username: offer.owner.username,
                OfferName: offer.name,
                category: offer.category
            }
        }, function(err, info) {
            if (err) {
                reject(err)
            } else {
                resolve(info)
            }
        });
    }));`enter code here`
}
Promise.all(promises).then(function(infos) { cb(null, infos) }, function(err) { cb(err) });

答案 1 :(得分:0)

目前,您正在发送一个电子邮件给收件人列表。我想您要做的是向列表中的每个收件人发送一封电子邮件,以便您可以为每个收件人设置问候语。

SellerMatched: function (fromUser, offer, cb) {

    for (var i=0; i<fromUser.length; i++)
    {
        mail.sendMail({
            from: 'Website Support <help@domain.com>',
            to: fromUser[i].email,
            subject: 'Seller Matched',
            template: 'SellerMatch',
            context: {
                user: fromUser[i].Name,
                username: offer.owner.username,
                OfferName: offer.name,
                category: offer.category
            }
        }, cb);
    }
}