I'm encountering difficulties when trying to include substitutions
data in emails sent from Sendgrid using Firebase Cloud Functions.
Here's my function
exports.firestoreEmail = functions.firestore
.document('users/{id}')
.onCreate(snap => {
const user = snap.data();
const msg = {
to: user.email,
from: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2b362f233e222b602d2123">[email protected]</a>',
subject: `${user.firstName}, please Verify Your Email Address`,
templateId: 'templateID',
substitutionWrappers: ['{{', '}}'],
substitutions: {
firstName: user.firstName,
email: user.email,
id: user.id
}
};
return sgMail
.send(msg)
.then(() => console.log('email sent!'))
.catch(err => console.log(err));
});
and the transactional template for the templateId
is
<html>
<head></head>
<body>{{firstName}} - {{email}} - {{id}}</body>
</html>
This results in an email being sent to the user.email
as planned, but with empty spaces where the substitutions
data should appear.
In line with the documentation and use cases provided here, I've also attempted to include
sgMail.setSubstitutionWrappers('{{', '}}');
to globally set the substitutionWrappers
. However, it still doesn't seem to work.
I've also used console.log(user)
which displays the data intended for the substitutions
in the console.
What am I overlooking? The data is accessible, the email format is correct, and the function closely aligns with the examples provided by SendGrid.