我正在尝试实现一种向多个收件人发送电子邮件的方法,但是我也想将具有收件人名称的user属性传递给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 <[email protected]>',
            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}}中,我只想显示收件人的名字,因为现在它显示了所有收件人的数组。

谢谢

最佳答案

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

SellerMatched: function (fromUser, offer, cb) {

    for (var i=0; i<fromUser.length; i++)
    {
        mail.sendMail({
            from: 'Website Support <[email protected]>',
            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);
    }
}

关于javascript - nodemailer多个收件人不同的上下文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38746600/

10-13 01:35