简体   繁体   English

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

[英]nodemailer multiple recipients different context

I am trying to implement a method to send emails to multiple recipients but also I want to pass to the html template the user attribute which will have the name of the recipient.我正在尝试实现一种向多个收件人发送电子邮件的方法,但我也想将具有收件人姓名的用户属性传递给 html 模板。 (I AM USING NODEMAILER as a nodejs module) (我使用 NODEMAILER 作为 nodejs 模块)

My code in this stage is like this:我这个阶段的代码是这样的:

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);
    }

and the html have those attributes: It's something like this:并且 html 具有这些属性:它是这样的:

...

Hi {{user}},

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

....

This is working properly, emails are sent to the recipients but in the attribute Hi {{user}}, I want to show only the name of recipient because now it shows the array of all recipients.这工作正常,电子邮件已发送给收件人,但在属性 Hi {{user}} 中,我只想显示收件人的姓名,因为现在它显示了所有收件人的数组。

Thanks谢谢

I saw in https://hashnode.com/post/nodemailer-multiple-recipients-different-context-ciret279f03hduy539u4tolck我在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) });

Right now you are sending one Email to a list of recipients.现在,您正在向收件人列表发送一封电子邮件。 I guess what you want to do is send one Email to each recipient in the list so you can set the greeting per recipient.我猜您想要做的是向列表中的每个收件人发送一封电子邮件,以便您可以为每个收件人设置问候语。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM