Trying to use transactional template with azure function sendgrid integration (javaScript) , I'm sending the following object (removed email address etc.):
const message =
{
"personalizations":[
{
"to":[
{
"email":"[MY_EMAIL]",
"name":"Rotem"
}
],
"dynamic_template_data":{
"rotem_test1":"wow"
}
}
],
"from":{
"email":"[FROM_EMAIL]",
"name":"name"
},
"reply_to":{
"email":"[REPLY_EMAIL]",
"name":"name"
},
"template_id":"[CORRECT_TEMPLATE_ID]",
"tracking_settings":{
"click_tracking":{
"enable":true
}
}
}
context.done(null,message);
also tried using context.done(null,JSON.stringify(message))
with the same result:
I get an email from the correct template but without any substitution.
when sending the exact same object using the https://api.sendgrid.com/v3/mail/send
API using postman everything works well.
would love to get help here as for what I'm doing wrong on my azure function.
With a SendGrid account created and SendGrid API key stored in an Azure AD B2C policy key, create a SendGrid dynamic transactional template. On the SendGrid site, open the transactional templates page and select Create a Dynamic Template. Enter a unique template name like Verification email and then select Create.
How to replace dynamic data (HTML) into SendGrid Templates in JavaScript How do create an email template in SendGrid? In the left navigation, click on the Email API - Dynamic Templates option diagram shown below It asks for a new dynamic template name as shown below. Once the template is created, You can create versions.
Azure Functions supports an output binding for SendGrid. This is reference information for Azure Functions developers. If you're new to Azure Functions, start with the following resources: Create your first function: C#, JavaScript, Java, or Python.
In the left navigation, click on the Email API - Dynamic Templates option diagram shown below It asks for a new dynamic template name as shown below. Once the template is created, You can create versions.
You did everything correctly as you can receive email as expected.
Problem is caused by the SDK version. Property dynamic_template_data
has just been added in latest 9.10.0 Sendgrid C# SDK, but the binding extension still uses old version, which has no idea what dynamic_template_data
is.
For 2.x function(Check Function app settings on Azure portal, see Runtime version: 2.xxx (~2)), we can install new version SDK manually before the extension is updated.
If you develop locally
<PackageReference Include="Sendgrid" Version="9.10.0" />
.func extensions install
to restore packages.Else on Azure portal
<PackageReference Include="Sendgrid" Version="9.10.0" />
and Save the changes.dotnet build extensions.csproj -o bin --no-incremental --packages D:\home\.nuget
Build succeeded
, start the function app.For 1.x function(Runtime ~1), I am afraid we have to manually send email using with sendgrid node module, Sendgrid vesion in 1.x function is locked therefore can't be updated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With