1

I'm trying to send a transactional email via node using sendGrid. Below is an example of my code.

        const subject = 'Email subject';
        const templateId = 'templateId';
        const sg = require('sendgrid')(secret);
        const request = sg.emptyRequest({
            method: 'POST',
            path: '/v3/mail/send',
            body: {
                "personalizations": [
                    {
                        "bcc": userEmails,
                        "substitutions": {
                            "-userName-": userDetails.name,
                            "-productPrice-": productDetails.price,
                            "-productUrl-": productDetails.url,
                            "-productPercentageDrop-": productDetails.percentageDrop,
                            "-productName-": productDetails.name,
                            "-productOriginalPrice-": productDetails.origPrice,
                            "-productDroppedPrice-": productDetails.dropPrice,
                            "-productImageUrl-": productDetails.imageUrl
                        },
                        "subject": subject.substring(0, 75)
                    }
                ],
                "from": {
                    "email": "myemail",
                    "name": "myname"
                },
                "content": [
                    {
                        "type": "text/html"
                    }
                ],
                "template_id": templateId
            }
        });

        sg.API(request, function (error, response) {
            if (error) {

                console.log('Error response received');
            }
            console.log(response.body.errors);
        });

But every time I run the code I get the following error message.

400 message: 'Bad Request', field: null, help: null

Which isn't really that helpful when trying to find out why its erroring.

Body JSON being sent:

{
"host":"",
"method":"POST",
"path":"/v3/mail/send",
"headers":{

},
"body":{
    "personalizations":[
        {
            "bcc":[
                {
                    "email":"name1@hotmail.com",
                    "name":"name1"
                },
                {
                    "email":"name2@hotmail.com",
                    "name":"name2"
                }
            ],
            "substitutions":{
                "-productPrice-":189.5,
                "-productUrl-":"http://www.tesco.com/direct/humax-fvp-4000t500-m-smart-freeview-play-hd-digital-tv-recorder-with-wi-fi-500gb/483-1785.prd",
                "-productName-":"Tesco direct: Humax FVP-4000T/500 (M) Smart Freeview Play HD Digital TV Recorder with Wi-Fi - 500GB"
            },
            "subject":"Product Tesco direct: Humax FVP-4000T/500 (M) Smart Freeview Play HD Digita"
        }
    ],
    "from":{
        "email":"email@pricetracker.io",
        "name":"Pricetracker"
    },
    "content":[
        {
            "type":"text/html"
        }
    ],
    "template_id":"XXXXXX"
},
"queryParams":{

},
"test":false,
"port":""

}

Tom Maton
  • 1,564
  • 3
  • 23
  • 41
  • Can you please post the raw JSON request body being sent? Does your template contain both a `subject` and `content` ? – bwest Nov 11 '16 at 16:05
  • @bwest added the JSON request – Tom Maton Nov 11 '16 at 16:11
  • Try removing the content parameter entirely – bwest Nov 11 '16 at 16:31
  • Just tried and unfortunately that didn't make any difference. – Tom Maton Nov 11 '16 at 16:35
  • Okay, and your template definitely has content specified in it? – bwest Nov 11 '16 at 16:36
  • I would also try adding a `value` to your text `content` part and see if that works, just to help pinpoint the error – bwest Nov 11 '16 at 16:53
  • @bwest I've just tried the other 2 suggestions and still getting the error message and in my template I'm referencing the following `-productPrice-` as above. – Tom Maton Nov 12 '16 at 12:34
  • Oh... I just noticed you don't have a To field, only BCC. Gotta have a To. – bwest Nov 12 '16 at 14:14
  • @bwest I've just updated following your latest comments eg replace the 'bcc' to 'to' and still getting the same error. – Tom Maton Nov 13 '16 at 20:38
  • Just to confirm, please post the contents of your template, and verify the template ID. If this doesn't work I ask that you please open a ticket with SendGrid support. – bwest Nov 14 '16 at 14:06
  • 1
    My other suggestion is to get the simplest possible use case working (one email, no subs, no replaceements), and then add each part until you can isolate where the problem is. – bwest Nov 14 '16 at 14:12
  • @bwest found the issue... it was this `"-productPrice-":189.5` it didn't like be passing through numbers, it has to be a string. Thanks for your help, I would give more points if I could. – Tom Maton Nov 14 '16 at 15:16

3 Answers3

1

don't know if this could still be helpful for you, but I've had some similar issues when using substitutions with Sendgrid. I'm using the sendgrid-php library, but internally it sends the same format. What I found was that all substitution values should be converted to string. For instance your value for productPrice should be like this:

"-productPrice-": "189.5",

With quotes. In my case I had integers, and when I converted them to strings, all worked correctly.

Cesar
  • 189
  • 8
  • Same for me as well. The error output was not saying anything. It was just giving `bad request 400`. Cascading the values to string just worked properly. The php api does not accept INT values. – Erce Dec 09 '22 at 10:41
0

If you are not passing string value to the template file then SendGrid will give you

{"errors":[{"message":"Bad Request","field":null,"help":null}]}

Convert your all non-string value to string before sending to the template file.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
0

I got same issue, then I fix using this below syntax, change only you message format.

"value": "'.html_entity_decode((str_replace('"',"'",$message))).'"
Tyler2P
  • 2,324
  • 26
  • 22
  • 31