Skip to content

Instantly share code, notes, and snippets.

@trouttdev
Last active November 16, 2016 08:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trouttdev/5eafb0d7b36d49ad2e4e to your computer and use it in GitHub Desktop.
Save trouttdev/5eafb0d7b36d49ad2e4e to your computer and use it in GitHub Desktop.
How to add CC and BCC fields to SparkPost API

How to send a SparkPost email via API with a CC and BCC

Originally I found this which only vaguely pointed me in the right direction, so I thought I'd create a gist of what ended up working for me.

CC

To add a CC address, you have to two two things

  1. Add the address to your recipeints array, and set the header_to value to an address in the to field. So if you're sending the email to to_address@domain.com and CCing it to cc_address@domain.com, the header_to for cc_address@domain.com will need to be set to to_address@domain.com
  2. Add the email to the CC headers option in content object. If you have multiple emails, these should be comma separated.

BCC

BCC Seems to be simpler than CC since it's only one step, you just need to repeat step #1 from CC and you're good to go.

  1. Add the address to your recipeints array, and set the header_to value to an address in the to field. So if you're sending the email to to_address@domain.com and BCCing it to bcc_address@domain.com, the header_to for bcc_address@domain.com will need to be set to to_address@domain.com

Exmaple

Below is an example which worked for me:

{
	"content": {
		"subject": "Test Message",
		"html": "This is a test message.",
		"headers": {
			"CC": "some_cc_address@domain.com,some_other_cc_address@domain.com" // add all CC addresses here, comma separated
		},
		"from": {
			"email": "some_from@domain.com",
			"name": "Test Sender"
		}
	},
	"recipients": [{
    		"address": {
    			"email": "to_address@domain.com",
    			"header_to": "to_address@domain.com"
    		}
    	}, {
    		"address": {
    			"email": "some_cc_address@domain.com",
    			"header_to": "to_address@domain.com" // note this address is the same as an address in the "to" field
    		}
       }, {
           "address": {
               "email": "some_other_cc_address@domain.com",
               "header_to": "to_address@domain.com" // note this address is the same as an address in the "to" field
           }
      }, {
          "address": {
              "email": "bcc_address@domain.com",
              "header_to": "to_address@domain.com" // note this address is the same as an address in the "to" field
          }
     }
    ],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment