2016-08-04 28 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

爲什麼我需要什麼時候不需要它? –

相關問題