I am using the latest Java API to set dynamic template data into my SendGrid Email Templates. However though i am passing the values correctly ( i am able to see the sent dynamic values in the web dashboard), yet the value is not getting 'expressed' when the mail is being generated.
I tried with both HTML editor as well as Code editor , following the steps in the documentation . Java code is as below.
personalization0.addTo(new Email("ron@xxxxx", "Ron Abraham"));
personalization0.addDynamicTemplateData("otpvalue", "123456");
mail.addPersonalization(personalization0);
mail.setTemplateId("<templateid>");
HTML Code in SendGrid web editor
<span style="color: #000; font-size: 18px">OTP Value : {{ otpvalue }} </span></div>
In the web dashboard , when i check the sent mail i see otpvalue is being passed to the template.
I guess there is somethign missing in the way the value gets written into the HTML Dynamic Template . Any pointers ?
EDIT - Pasting the full method code below
@PostMapping("/sendDynamicMail")
@PreAuthorize("#oauth2.hasScope('signup')")
public Response sendDynamicMailviaSendGridApi() throws IOException{
String API_KEY = "SG.**********";
Email from = new Email("*****@*****.com");
String subject = "Test Dynamic Mail from Development team";
Email to = new Email("ron@*****.com");
Content content = new Content("text/html","Replace Body Content with Dynamic Template");
Mail mail = new Mail(from,subject,to,content);
mail.setReplyTo(from);
Personalization personalization0 = new Personalization();
personalization0.addTo(new Email("ron+1@*****", "Ron Abraham"));
personalization0.addDynamicTemplateData("otpvalue", "666666");
mail.addPersonalization(personalization0);
//design first one template
mail.setTemplateId("d-6dc6223eebfc49b7b887cac1c0f34fa2");
SendGrid sg = new SendGrid(API_KEY);
Request request = new Request();
Response response;
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
return response;
}
catch(IOException io) {
throw io;
}
finally {
//do nothing
}
}
Here is how the HTML Dynamic Template Looks . I have tried using the otp value in several places.
<div style="font-family: inherit; text-align: center"> <span style="color: #000; font-size: 18px">OTP Value1 : {{ otpvalue }} </span></div>
the issue was how the personalization data was being passed.
personalization0.addDynamicTemplateData("dynamic_template_data", "{otpvalue:454545}" );
earlier i was passing the otp value directly as as key value pair.
On analyzing the format which was being sent via curl , i made the change in java and it has worked.