Search code examples
azureazure-functionssendgridsendgrid-templates

dynamic_template_data doesn't work with sendgrid and azure function integration


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.


Solution

  • 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

    1. Go to function project directory, delete bin, obj folder.
    2. Edit extensions.csproj under function project, add latest version Sendgrid <PackageReference Include="Sendgrid" Version="9.10.0" />.
    3. In this directory open console(terminal,Powershell,etc), input func extensions install to restore packages.

    Else on Azure portal

    1. Stop the function app.
    2. Access kudu console through Platform Features -> Advanced Tools(Kudu) -> Debug Console (CMD)
    3. Navigate to D:\home\site\wwwroot
    4. Delete the bin directory
    5. Edit extensions.csproj, add <PackageReference Include="Sendgrid" Version="9.10.0" /> and Save the changes.
    6. In console below, input dotnet build extensions.csproj -o bin --no-incremental --packages D:\home\.nuget
    7. After you see 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.