2017-08-18 61 views
1

這裏是我的傳送對象:如何在沒有CC和/或BCC受體的情況下使用sparkpost?

if(req.body.bcc === ''){ 
 
    req.body.bcc = ''; 
 
    } 
 
    if (req.body.cc === '') { 
 
    req.body.cc = ''; 
 
    } 
 
    if (req.body.subject === '') { 
 
    req.body.subject = 'No subject'; 
 
    } 
 
    if (req.body.source === '') { 
 
    req.body.source = '<EMAIL OMITTED>'; 
 
    } 
 
    if (req.body.messageBody === '') { 
 
    req.body.messageBody = 'No body text has been entered'; 
 
    } 
 
    var transmission = { 
 
    recipients: [ 
 
     { 
 
     address: { 
 
      email: req.body.to, 
 
      name: 'To recipient' 
 
     }, 
 
     substitution_data: { 
 
      recipient_type: 'Original' 
 
     } 
 
     } 
 
    ], 
 
    cc: [ 
 
     { 
 
     address: { 
 
      email: req.body.cc, 
 
     }, 
 
     substitution_data: { 
 
      recipient_type: 'CC' 
 
     } 
 
     } 
 
    ], 
 
    bcc: [ 
 
     { 
 
     address: { 
 
      email: req.body.bcc, 
 
     }, 
 
     substitution_data: { 
 
      recipient_type: 'BCC' 
 
     } 
 
     } 
 
    ], 
 
    content: { 
 
     from: { 
 
     name: req.body.source, 
 
     email: req.body.source 
 
     }, 
 
     subject: req.body.subject, 
 
     text: req.body.messageBody, 
 
     html: `<p></p>` 
 
    } 
 
    };

我有送輸入數據req.body HTML輸入形式,但如果沒有req.body.cc或req.body.bcc內容,會發生什麼情況是Sparkpost拋出一個錯誤說不可訪問實體:

name: 'SparkPostError', 
 
    errors: 
 
    [ { message: 'Invalid header', 
 
     description: 'Error while validating header CC: Missing header content', 
 
     code: '3002' } ], 
 
    statusCode: 422 }

如果我在cc和bcc字段中放置了一個隨機數字1,則該消息發送給「to」用戶,但是sparkpost告訴我該消息未能發送到2個接收者。我覺得我在這裏錯過了一些東西,因爲如果沒有bcc或cc收件人,那麼它應該發送到「to」字段中的電子郵件,我不應該在cc或bcc中輸入隨機亂碼來獲得它發送電子郵件。有人遇到這個問題或有一個想法,我可以如何解決這個問題?

也許我可以做一些檢查,如果該字段爲空,請將其替換爲某個默認值,sparkpost會知道我並不試圖向任何人發送電子郵件,例如佔位符。但是如果有這樣的事情,我還沒有發現它。

+2

只要避免在傳輸對象爲空時創建cc和bcc屬性, – HungryCoder

回答

2

以下是發送沒有CC或BCC的電子郵件的示例。如果您沒有任何BCC或CC收件人而不包含其數組對象。

{ 
    "options": { 
     "open_tracking": true, 
     "click_tracking": true 
    }, 
    "campaign_id": "test", 
    "recipients": [ 
    { 
     "address": { 
     "email": "[email protected]", 
     "name": "To recipient" 
     }, 
     "tags": [], 
     "substitution_data": {"recipient_type": "Original"} 
    } 
    ], 
    "content": { 
    "from": { 
     "email": "[email protected]", 
     "name": "From address" 
    }, 
    "subject": "My Sample Subject", 
    "text": "{{recipient_type}}", 
    "reply_to": "[email protected]", 
    "html": "<p>{{recipient_type}}</p>" 
    } 
} 

這裏的例子作爲curl命令:

curl -X POST \ 
    https://api.sparkpost.com/api/v1/transmissions \ 
    -H 'authorization: YOUR_API_KEY_HERE' \ 
    -H 'cache-control: no-cache' \ 
    -d '{ 
    "options": { 
     "open_tracking": true, 
     "click_tracking": true 
    }, 
    "campaign_id": "test", 
    "recipients": [ 
    { 
     "address": { 
     "email": "[email protected]", 
     "name": "To recipient" 
     }, 
     "tags": [], 
     "substitution_data": {"recipient_type": "Original"} 
    } 
    ], 
    "content": { 
    "from": { 
     "email": "[email protected]", 
     "name": "From address" 
    }, 
    "subject": "My Sample Subject", 
    "text": "{{recipient_type}}", 
    "reply_to": "[email protected]", 
    "html": "<p>{{recipient_type}}</p>" 
    } 
} 

' 

你需要把你的API密鑰和替換到/從地址有適合您的測試用例的。

相關問題