3

I have templates created in SparkPost dashboard. But the problem I face is that I am not able to send "CC" or "BCC" by making the api calls. The code snippet below will help you understand what I am trying to do.

var SPARKPOST_KEY = "KEY"
var sparkpost = require('sparkpost');
var sparkclient = new sparkpost(SPARKPOST_KEY);

var req_opts = {
 transmissionBody : {
  content: {
   template_id: 'order-confirmation',
   from: 'support@domain.in',
        subject: 'Order confirmation',
        headers: {
         "CC": "<anon2@gmail.com>"
        }
  },
  substitution_data: {
   "CC": "anon2@gmail.com",
   "customer": "Aravind",
   "order": 123532
  },
  recipients: [
        {address: {email:'anon1@domain1.in'}},
        {address: {email: 'anon2@gmail.com'}}
     ],
     "return_path": "support@domain.in",
 }
};

sparkclient.transmissions.send(req_opts, function(err, res){
 if(err){
  console.log("ERROR");
  console.log(err)
 }else {
  console.log(res.body);
  console.log("Mail has been successfully sent");
 }
});
Grokify
  • 15,092
  • 6
  • 60
  • 81

1 Answers1

2

As mentioned in the reply on your github issue, you must use either inline content or a template. So as the documentation says, use just template_id in your content.

What needs to happen for this to work is that the headers in the template include a CC header, as described here. Currently there is no way to set the headers of a template in the UI -- it must be done using the API.

To do this execute a PUT against the templates endpoint, in your case https://api.sparkpost.com/api/v1/templates/order-confirmation, with a JSON payload containing the following:

{
  "content": {
    <other content parts>
    "headers": {
      "CC": "{{CC}}"
    }
  }
}

Note that you will also need to use the header_to parameter for your CC recipient to prevent their address showing up in the To: header. In your example this means replacing:

{address: {email: 'anon2@gmail.com'}}

with this:

{address: {email: 'anon2@gmail.com', header_to: 'anon1@domain1.in'}}

You also do not need the return_path parameter.

Hope this helps!

orval
  • 97
  • 4
  • Doesn't seem to work with templates. Its shooting an error : `{ "errors": [ { "message": "required field is missing", "description": "At least one of 'text' or 'html' needs to exist in 'content'", "code": "1400" } ] }` – Bharath Vijay Aug 07 '16 at 18:22
  • Your template needs to include a `text` or an `html` part within `content`. – orval Aug 15 '16 at 11:13
  • Why do I need to when there is no need for it ? – Bharath Vijay Aug 17 '16 at 07:02
  • There is a need for it. Your message needs content. – orval Aug 18 '16 at 12:09
  • Can you please give me a code example in the answer ? Because all of my contents are already in the templates – Bharath Vijay Aug 19 '16 at 19:07
  • This is what I have in my JavaScript test code: ```var req_opts = { transmissionBody : { content: { template_id: '' }, substitution_data: { "CC": "" }, recipients: [ {address: {email: ''}}, {address: {email: '', header_to: ''}} ] } }; ``` My template content has these fields: `from`, `headers`, `html`, and `subject`. – orval Aug 22 '16 at 10:11
  • Also worth mentioning - if you issue a PUT to update a template property through the API because it isn't visible in the SparkPost admin UI and then later make a change to the template through the SparkPost admin UI, it doesn't overwrite the change you originally made through the API. – benb1n May 09 '19 at 14:07