2016-08-04 35 views
2

我在sparkpost仪表板中创建了模板。但是我面对的问题是,我无法通过进行api调用来发送“CC”或“BCC”。下面的代码片段将帮助您了解我正在尝试执行的操作。调用节点sparkpost API时在替代数据中发送“CC”和“BCC”

var SPARKPOST_KEY = "KEY" 
 
var sparkpost = require('sparkpost'); 
 
var sparkclient = new sparkpost(SPARKPOST_KEY); 
 

 
var req_opts = { 
 
\t transmissionBody : { 
 
\t \t content: { 
 
\t \t \t template_id: 'order-confirmation', 
 
\t \t \t from: '[email protected]', 
 
     \t \t subject: 'Order confirmation', 
 
     \t \t headers: { 
 
     \t \t \t "CC": "<[email protected]>" 
 
     \t \t } 
 
\t \t }, 
 
\t \t substitution_data: { 
 
\t \t \t "CC": "[email protected]", 
 
\t \t \t "customer": "Aravind", 
 
\t \t \t "order": 123532 
 
\t \t }, 
 
\t \t recipients: [ 
 
     \t \t {address: {email:'[email protected]'}}, 
 
     \t \t {address: {email: '[email protected]'}} 
 
    \t ], 
 
    \t "return_path": "[email protected]", 
 
\t } 
 
}; 
 

 
sparkclient.transmissions.send(req_opts, function(err, res){ 
 
\t if(err){ 
 
\t \t console.log("ERROR"); 
 
\t \t console.log(err) 
 
\t }else { 
 
\t \t console.log(res.body); 
 
\t \t console.log("Mail has been successfully sent"); 
 
\t } 
 
});

回答

1

如在回答你的github issue提到的,你必须使用内嵌的内容或模板。正如documentation所述,在您的content中只使用template_id

这需要发生什么工作是模板中的headers包括CC标头,如here所述。目前无法在用户界面中设置模板的标题 - 必须使用API​​完成。

为此执行PUT对templates endpoint,你的情况https://api.sparkpost.com/api/v1/templates/order-confirmation,包含以下内容的JSON有效载荷:

{ 
    "content": { 
    <other content parts> 
    "headers": { 
     "CC": "{{CC}}" 
    } 
    } 
} 

请注意,您还需要使用header_to参数为您的CC收件人阻止他们的地址显示在To:标题中。在您的例子,这意味着更换:

{address: {email: '[email protected]'}} 

与此:

{address: {email: '[email protected]', header_to: '[email protected]'}} 

你也不需要return_path参数。

希望这会有所帮助!

+0

似乎不适用于模板。其拍摄错误:“{ ”错误“:[ { ”message“:”必填字段丢失“, ”description“:”'content'中至少有一个'text'或'html'存在'', “code”:“1400” } ] }' –

+0

您的模板需要在'content'内包含'text'或'html'部分。 – orval

+0

为什么我需要什么时候不需要它? –

相关问题