I'm attempting to integrate with Sendgrid and am having a heck of a time. I have created a dynamic template - a new one, not a legacy one - with a single handle bar (first_name) in it. It has a subject. But I'm getting a load of errors that I could use some help with.
First the code:
public void sendEmail(String toEmail, String toName) throws IOException {
String fromEmail = "[email protected]";
String fromName = "Blah blah";
SendGrid sendGrid = new SendGrid("the_api_key");
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
String body = "see below...";
request.setBody(body);
Response response = sendGrid.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
Taken almost entirely from the Java example code.
The JSON body, pretty printed...
{
"from": {
"email": "[email protected]",
"name": "Blah blah"
},
"personalizations": [
{
"to": [
{
"email": "[email protected]",
"name": "Blah Blah"
}
],
"dynamic_template_data": {
"first_name": "Babaloo"
}
}
],
"template_id": "[d-lotsandlotsofcharacters]"
}
And then a bunch of errors that make no sense (all of which link to a 404):
- Cannot use dynamic template data with a legacy template ID -
I'm not using a legacy template id according to the UI - The template_id must be a valid GUID, you provided '[d-xxxxxxxxxxxxxx]' -
I sent what I was given on the UI. - The subject is required. You can get around this requirement if you use a template with a subject defined or if every personalization has a subject defined. -
My template has a subject - Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required. -
A valid template_id was provided
I'm guessing the first issue is the template_id field. It's strange JSON in that the value includes the array open/close. Putting the value inside as text gives a parse error so Sendgrid must be taking that directly.
Any directional help would be most appreciated. The docs are rather challenging
d-xxxxxxxetc. - philnash