When sending a mail with template_id an error is thrown:
{"errors":[{"message":"Bad Request","field":null,"help":null}]}
{ server: 'nginx',
date: 'Sun, 10 Jul 2016 17:51:30 GMT',
'content-type': 'application/json',
'content-length': '63'}
Use this function to send an email:
sendTemplate(recipients, templateId, substitutions) {
const sg = require('sendgrid').SendGrid('XXXXXXXXXX');
const requestPost = sg.emptyRequest();
requestPost.body = {
from: { email: '[email protected]' },
personalizations: [
{
to: _.map(recipients, (recipientAddress) => {
return {email: recipientAddress};
}),
substitutions: _.transform(substitutions, (result, values, key) => {
result[key] = values;
}, {})
}
],
template_id: templateId
};
requestPost.method = 'POST';
requestPost.path = '/v3/mail/send';
sg.API(requestPost, function (response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});
}
When:
recipients - is an array of email adresses
templateId - a template id
substitutions - key,value pairs where key is a string and value is an array of values with a length of the recipients array.
Removing the template_id and substitutions from personalizations object and set a content and a subject instead - works as expected.
It seems that personalizations.substitutions accepts only a <key,value> pairs where the value is NOT an array.
When passing {'-username-': 'testUser'} it works (although he is asking for a content property that, IMO, shouldn't be there in case of a template).
When passing {'-username-': ['testUser']} it fails.
This is weird because according to substitution_tags API it seems to accept multiple values for a substitution (with respect to the tos array size).
If I got the API wrong, please close this issue (and I would appreciate if you tell me where I was wrong..)
Thanks.
Hello @akaspi,
The substitutions used to be an array that matched index for index with the recipients array. Now, each substitution goes to a personalization.
Previously, you would do the following:
to: email1, email2, email3
substitutions: name = sam, jane, mike
Now, you would do the following:
personalization1:
personalization2:
personalization3:
etc..
For more on the changes between v2 and v3, please review: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/how_to_migrate_from_v2_to_v3_mail_send.html
I hope that helps. If you need further clarification, please let me know.
Hmm, I've also just spent 2 hours debugging this, because the documentation here uses arrays for substitutions, and the documentation here does not detail how substitutions are supposed to be structured.
@thinkingserious Could you see if the docs can be updated to reflect what you wrote above?
Also, the API still errors on this as BadRequest with field: null / help: null, which isn't very helpful :(
Thanks for looking deeper into this @adamreisnz. I've added this to our backlog for further investigation and documentation update.
The current example for sending attachments is here: https://github.com/sendgrid/sendgrid-nodejs/blob/master/USE_CASES.md#transactional_templates
That example will update, when this is merged: https://github.com/sendgrid/sendgrid-nodejs/pull/378
check this link https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions
Check All values of custom arguments object must be strings
Check if the mail is you are sending is in array the even substitutions should be in array
Thanks @srinath-patel , this string value was the issue for me, i had one value integer.
Error returned by api request should be more specific. It wasted a few hours of mine to debug this.
Hello @ziaulrehman40,
I agree with you. Do you mind creating a new issue for this? Or, I can create if you like.
With Best Regards,
Elmer
@thinkingserious sure i can, though i am not sure where to, as i am a rails dev and faced this issue on sendgrid-ruby client. This nodejs one is also a client, this issue sounds more like a server side issue where server needs to return some proper error and not clients. Obviously not aware of implementation of clients, they might also need updates for this. So just let me know where and i will open an issue for this. Thanks.
Hi @ziaulrehman40,
In ruby, if you were to catch the error, you should be able to see a more sane message like so.
With Best Regards,
Elmer
Well, while on it. For me, ruby client was not raising any errors, instead response returned can contain error messages. And in that, for this specific issue of non-string values, it had error null field null. So anyway, my point was that for this specific issue error messages should be properly populated instead of error null response. Please open ticket for this wherever you see fit. Thanks.
Ah, thank you for the clarification! In that case, please follow this issue for progress. Thanks!
Most helpful comment
check this link https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions
Check All values of custom arguments object must be strings
Check if the mail is you are sending is in array the even substitutions should be in array