2016-11-11 76 views
0

我试图通过使用sendGrid的节点发送交易电子邮件。以下是我的代码示例。尝试使用sendGrid从模板发送交易电子邮件时出错

 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); 
     }); 

但是,每次运行代码时,我都会收到以下错误消息。

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

试图找出为什么它的示数时,这是不是真的那么有用。

身体JSON发送:

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

}, 
"body":{ 
    "personalizations":[ 
     { 
      "bcc":[ 
       { 
        "email":"[email protected]", 
        "name":"name1" 
       }, 
       { 
        "email":"[email protected]", 
        "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 protected]", 
     "name":"Pricetracker" 
    }, 
    "content":[ 
     { 
      "type":"text/html" 
     } 
    ], 
    "template_id":"XXXXXX" 
}, 
"queryParams":{ 

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

}

+0

您可以请发送正在发送的原始JSON请求正文吗?您的模板是否同时包含“主题”和“内容”? – bwest

+0

@bwest添加了JSON请求 –

+0

尝试完全删除内容参数 – bwest

回答

0

不知道这是否仍然可以对你有所帮助,但我一直在使用与Sendgrid换人时有一些类似的问题。我使用sendgrid-php库,但在内部它发送相同的格式。我发现所有替代值都应该转换为字符串。例如,您对productPrice的价值应如下所示:

"-productPrice-": "189.5", 

用引号引起来。在我的情况下,我有整数,当我将它们转换为字符串时,所有工作都正常。

相关问题