【问题标题】:How to use SparkPost without a CC and/or BCC recipient?如何在没有 CC 和/或 BCC 收件人的情况下使用 SparkPost?
【发布时间】:2017-08-18 01:11:08
【问题描述】:

这是我的传输对象:

  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.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 之类的随机数,则消息会发送给“收件人”用户,但 sparkpost 告诉我消息无法发送给 2 个收件人。我觉得我在这里遗漏了一些东西,因为如果没有密件抄送或抄送收件人,那么它应该只发送到“收件人”字段中的电子邮件,我不应该在抄送或密件抄送中输入随机乱码来得到它发送电子邮件。有人遇到过这个问题或知道我该如何解决这个问题吗?

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

【问题讨论】:

  • 只要避免在 transmission 对象为空时创建 cc 和 bcc 属性即可。

标签: javascript node.js sparkpost


【解决方案1】:

这里是一个不使用抄送或密件抄送发送电子邮件的示例。如果您没有任何密件抄送或抄送收件人,则不要包含他们的数组对象。

{
    "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 密钥,并用适合您的测试用例的地址替换收件人地址。

【讨论】:

    猜你喜欢
    • 2021-01-21
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2016-12-10
    • 2021-03-03
    • 1970-01-01
    • 2010-12-05
    相关资源
    最近更新 更多