javascriptnode.jssparkpost

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


Here's my transmission object:

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

I have HTML input forms that send the entered data to req.body, but if there's no req.body.cc or req.body.bcc content, what happens is SparkPost throws an error saying Unaccessible entity and:

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

If I put a random number like 1 in the cc and bcc fields, then the message sends to the "to" user but sparkpost tells me that the message failed to send to 2 recepients. I feel like I'm missing something here because if there is no bcc or cc recipients then it should just send to the email in the "to" field and I shouldn't have to enter random gibberish in cc or bcc to get it to send an email. Has someone ran into this issue or have an idea how I could solve this problem?

Maybe I can do some check where if the field is blank, replace it with some default value that sparkpost will know that I'm not trying to send an email to anyone there, like a placeholder. But if there is such a thing, I haven't found it yet.


Solution

  • Here is an example of sending an email without CC or BCC. If you do not have any BCC or CC recipients than don't include their array objects.

    {
        "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>"
      }
    }
    

    Here is the example as a curl command:

    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>"
      }
    }
    
    '
    

    You will need to put your API key and replace the to/from addresses with ones appropriate for your test case.