Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 328
Rotem Slootzky Avatar asked Oct 23 '18 15:10

Rotem Slootzky


People also ask

How do I create a SendGrid dynamic transactional template?

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 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.

Does Azure Functions support SendGrid output binding?

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.

How to create dynamic templates in the email API?

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.


1 Answers

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.

like image 50
Jerry Liu Avatar answered Oct 21 '22 18:10

Jerry Liu