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

时间:2017-08-18 01:11:08

标签: javascript node.js 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>`
    }
  };

我有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 }

如果我在cc和bcc字段中放置一个像1这样的随机数,那么该消息将发送给“to”用户,但是sparkpost告诉我该消息未能发送给2个接收者。我觉得我在这里遗漏了一些东西,因为如果没有密件抄送或抄送收件人,那么它应该只发送到“收件人”字段中的电子邮件,我不应该输入cc或bcc中的随机乱码来获取它发送电子邮件。是否有人遇到过这个问题,或者知道如何解决这个问题?

也许我可以检查一下,如果该字段为空,请将其替换为某个默认值,即sparkpost将知道我不会尝试向其中的任何人发送电子邮件,例如占位符。但如果有这样的事情,我还没有找到它。

1 个答案:

答案 0 :(得分:2)

以下是发送不带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密钥,并将地址替换为适合您的测试用例的地址。