如何在没有 CC and/or BCC 收件人的情况下使用 SparkPost?

How to use SparkPost without a CC and/or BCC recipient?

这是我的运行任务对象:

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

我有 HTML 输入表单将输入的数据发送到 req.body,但是如果没有 req.body.ccreq.body.bcc 内容,SparkPost 会抛出错误说无法访问的实体和:

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

如果我在抄送和密件抄送字段中输入一个 运行dom 号码,例如 1,那么消息会发送给 "to" 用户,但 sparkpost 告诉我消息无法发送给 2 个收件人.我觉得我在这里遗漏了一些东西,因为如果没有 bcc 或 cc 收件人,那么它应该只发送到 "to" 字段中的电子邮件,我不应该输入 运行dom 乱码抄送或密件抄送让它发送电子邮件。有人 运行 解决过这个问题或者知道我该如何解决这个问题吗?

也许我可以做一些检查,如果该字段为空,将其替换为一些默认值,sparkpost 会知道我不会尝试向那里的任何人发送电子邮件,例如占位符。不过如果真有这种东西,我还没找到呢

这里是一个发送没有抄送或密件抄送的电子邮件的例子。如果您没有任何 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 密钥并将 to/from 地址替换为适合您的测试用例的地址。