如何在没有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>` } }; 

我有HTMLinput表单,将input的数据发送到req.body,但是如果没有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个接收者。 我觉得我在这里错过了一些东西,因为如果没有密件抄送或抄送收件人,那么它应该只是发送到“到”字段的电子邮件,我不应该input随机乱序抄送或密件抄送到达发送电子邮件。 有人遇到这个问题,或有一个想法,我怎么能解决这个问题?

也许我可以做一些检查,如果该字段是空白的,用一些默认值replace它,sparkpost会知道我不是试图发送电子邮件给任何人,如占位符。 但是如果有这样的事情,我还没有find。

这是一个发送电子邮件没有CC或BCC的例子。 如果您没有任何BCC或CC收件人而不包含其数组对象。

 { "options": { "open_tracking": true, "click_tracking": true }, "campaign_id": "test", "recipients": [ { "address": { "email": "to@example.com", "name": "To recipient" }, "tags": [], "substitution_data": {"recipient_type": "Original"} } ], "content": { "from": { "email": "from@example.com", "name": "From address" }, "subject": "My Sample Subject", "text": "{{recipient_type}}", "reply_to": "test@example.com", "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": "to@example.com", "name": "To recipient" }, "tags": [], "substitution_data": {"recipient_type": "Original"} } ], "content": { "from": { "email": "from@example.com", "name": "From address" }, "subject": "My Sample Subject", "text": "{{recipient_type}}", "reply_to": "test@example.com", "html": "<p>{{recipient_type}}</p>" } } ' 

您将需要放置您的API密钥,并使用适合您的testing用例的地址replace地址/地址。