After the Mandrill that many developers grew to know and love was swallowed up by its parent MailChimp, many of us were looking for an alternative.
SparkPost emerged as what seemed like the next best thing due to its easy to use API and a generous free tier. The problem that I, and many other developers, ran into was that there is almost no documentation on how to add CC or BCC to an API response.
After some digging and a lot of trial an error, I finally found the right combination of things that worked.
CC
To add a CC address, you have to two two things
- Add the address to your recipients array, and set the header_to value to an address in the to field. For example, if you’re sending the email to to_address@domain.com and CCing it to cc_address@domain.com , then the header_to for cc_address@domain.com will need to be set to to_address@domain.com
- Add the email to the CC headers option in content object. If you have multiple emails, these should be comma separated.
I know that’s a little confusing. See the example at the bottom of this post.
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.
- Add the address to your recipients 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
Again, I know that’s confusing. See the example below.
Sample Request
{ "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 } } ], }