We're using Sendgrid v3 /mail/send
to trigger warning emails to our team about events. However, I can't get the data to appear in our emails.
Our workflow goes like this
- cron trigger (
startTrigger
) - postgres query returning tuples with fields
name
andamount
(getLowBalanceBountiesQuery
) - execute JS to format the data for sendgrid (
fmtSendgrid
) - use sendgrid resource query to send request to their API (
sendEmail
)
Our fmtSengrid
block executes the following (some fields modified here for privacy):
return JSON.stringify({
template_id: "d-our-template-id",
personalizations: [
{
to: [
{
email: "email@email.org"
}
]
}
],
from: {
email: "email@email.org"
},
// this is the key part that is not working
dynamic_template_data: { bounties: getLowBalanceBountiesQuery.data}
})
We then use {{fmtSendgrid.data}}
as the body for sendEmail
.
I've been able to confirm that:
-
getLowBalanceBountiesQuery.data
has the expected records in it (i.e. we aren't runningfmtSendgrid
on a falsy) - Sendgrid delivers the correct dynamic template email to the recipient but with no dynamic data
- Whether or not we use
JSON.stringify()
in our formatting function, the resulting email contains no data.
For reference, we have Sendgrid test data that renders successfully in our dynamic template. Here's the template:
<!-- Template -->
{{#if bounties}}
<ol>
{{#each bounties}}
<li>{{{this.name}}}: <b>{{{this.amount}}} remaining</b></li>
{{/each}}
</ol>
{{else}}
<p>No bounties</p>
{{/if}}
and our Sendgrid test data:
{
"bounties": [
{"name": "Sendgrid test 1", "amount": 3500},
{"name": "Sendgrid test 2", "amount": 2300}
]
}
I think the issue is the way the data is being serialized, but I also have a suspicion that the JS that creates the JSON that is transmitted to Sendgrid may not have been evaluated, because sometimes the hover over {{fmtSendgrid.data}}
displays as null, even though our the JS was executed on data from a successful database query.
How can we reconcile the data passed to dynamic_template_data
with the test data we're using in Sendgrid?