0
votes

I working with sendGrid Mail Api to send mail using firebase cloud functions

Here is my code for sending email using sendGrid/Mail API

 studentList.forEach(s => {
            var e = {
                to: s.email,
                from: '[email protected]',
                subject: "Mail Using Send Grid",
                templateId: templateId,
                substitutionWrappers: ['{{', '}}'],
                substitutions: {
                   name: s.name
                }
            }
            mailArray.push(e);
        });
        return sendGrid.send(mailArray).then(() => {
            var message = { message: "Mail Successfully sent" };
            return response.status(200).send(message);
        });

My template in send Grid See the image

Mail is sent but the placeholders are not replaced with substitutionWrappers. Please help me in, as i'm new to this.

1

1 Answers

0
votes

Install the latest version of @sendgrid/mail package and follow the instructions on following link on official documentation Transactional Templates Use Case

The thing is now you have to use dynamic_template_data instead of substitutions. You also can delete the substitutionWrappers property,because starting with v3 API, there's no need to specify the substitution wrappers as it will assume that you're using {{ curly braces}}.

Here is an example that should work:

constudentList.forEach(s => {
        var e = {
            to: s.email,
            from: '[email protected]',
            subject: "Mail Using Send Grid",
            templateId: templateId,
            dynamic_template_data: {
               name: s.name
            }
        }
        mailArray.push(e);
    });
    return sendGrid.send(mailArray).then(() => {
        var message = { message: "Mail Successfully sent" };
        return response.status(200).send(message);
    });