在 N 对 N 关系上发送电子邮件时的 SendGrid 个性化

SendGrid personalization when sending emails on N to N relationship

我正在尝试实施电子邮件价格提醒系统。在我的情况下,一封电子邮件可以有多个价格警报,因此,在发送警报时与 SendGrid 发生冲突,因为它收到以下错误:

message: 'Each email address in the personalization block should be ' +
        'unique between to, cc, and bcc. We found the first duplicate ' +
        'instance of [xxxxxxx@gmail.com] in the personalizations.0.to ' +
        'field.',
      field: 'personalizations.0',
      help: 'http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.recipient-errors'

我还没有找到任何可以展示我的用例的东西。我得到的最好的办法是将同一封电子邮件发送给多个不同的收件人,例如:https://sendgrid.com/docs/for-developers/sending-email/personalizations/#sending-the-same-email-to-multiple-recipients

这可以用 SendGrid 实现吗?!

我的函数:

function sendEmail (emailz, subject, body) {
    const email = {
        to: emailz,
        from: 'salut@xxxxxxxxx.ro',
        subject: subject,
        text: body,
        html: body
    }

    return sgMail.send(email)
                .catch(error => {
                    console.error(error)
                    if (error.response) {
                        const {message, code, response} = error;
                        const {headers, body} = response;
                        console.error(body);
                    }
                });
}

实际电子邮件发送完成:

            try { 
                    await sendEmail(
                            emailul,
                            'Alerta notificare pret!!!',
                            `Pretul produsului pe care il urmaresti este de ${pretulCurent} , sub alerta de pret setata de ${pretulDorit}. <br /> Produsul tau este: ${titlul} : ${link} `
                        )
                } catch (e) {
                    await sendEmail('S-a produs o eroare', e.message)
                    throw e
                }

如有任何帮助,我们将不胜感激。如果使用 SendGrid 无法做到这一点,您能给我一些可能适用于这种情况的其他服务的指示吗?

好的,如果我理解正确的话,这里的问题似乎是避免在您的 to 电子邮件字段中重复。我建议您在推送到 SendGrid 之前 检查重复项(SendGrid 不会为您做这件事每个 batch/job.

SendGrid 接受字符串数组或以逗号分隔的值,因此您可以尝试这样的操作:

var finalEmailul = [];
// for each email in the list, check - if it doesn't exist, push it in
if(finalEmailul.indexOf(emailul) < 0){
    finalEmailul.push(emailul);
};

// then use 'finalEmailul' in your 'sendEmail' function
try { 
    await sendEmail(
        finalEmailul,   // <- use here. If you want it to be comma-delimited, use finalEmailul.join(",")
        'Alerta notificare pret!!!',
        `Pretul produsului pe care il urmaresti este de ${pretulCurent} , sub alerta de pret setata de ${pretulDorit}. <br /> Produsul tau este: ${titlul} : ${link} `
    )
} catch (e) {
    await sendEmail('S-a produs o eroare', e.message)
    throw e
}