1

I need to use the email that will be the recipient of the sendgrid template email inside the template itself. I could send it as a parameter, but it isn't desirable as I can send (By API) one only request with a array of recipients. How do I use the recipient email as a parameter inside the template?

Something like {{{recipient}}}...

The email are being sent by a NodeJS Express back-end in the following way:

public async sendTransactionalMail(title: string, template_id: string, recipients: string[], template_data: any) {
    const template_data_to_send = {...template_data, subject: title};
    const msg = {
        to: recipients,
        from: 'email@email.com',
        templateId: template_id,
        dynamicTemplateData: template_data_to_send
    };

    return await mail.send(msg);
}
  • 1
    I'm not sure if I understand the question. What is wrong with adding the recipient as a parameter of the `.send()` function? – IObert Mar 22 '23 at 07:49
  • I need that the email to which was sent be shown inside the email body. The Sendgrid API accepts a array of strings as a "recipient", so I can send email to more than one person with only one request. But if I just get that string array and pass it by parameter, in the dynamic template I would not know which email of this array is being sent, so I can't put the actual email address in the template – Richard Lucas Mar 22 '23 at 13:39
  • I'm trying to do it without sending multiple API requests, if it's possible. – Richard Lucas Mar 22 '23 at 13:39
  • 1
    I'm still not 100% sure but I think you might be looking for the [Personalization](https://docs.sendgrid.com/for-developers/sending-email/personalizations) feature in which you can use different objects for each recipient. – IObert Mar 22 '23 at 15:08

0 Answers0