Send individual emails to multiple recipients without using BCC with the SparkPost API

835 Views Asked by At

Our Django app uses SparkPost as an email provider. A new feature we are implementing should allow users to create their own organizational emails and send them to whoever they wish. Now, these emails should be received as individual ones, not with multiple recipients ("to") so that users can't see each other's address.

I have run a few tests with the SparkPost transmissions API. This is how you send an email:

sp = SparkPost(API_KEY)
response = sp.transmissions.send(recipients=emails, html=body, from_email=sender, subject=self.subject)

Where emails is a list of string literals.

In all test cases except one I did get individual emails with a single recipient just as I was after. But in one case the email had multiple "to" emails, and you could see each other's email address. I changed absolutely nothing in the code, this just happened.

Is there any way I could do that other than sending an individual transmission for each recipient? I'm worried about performance if it comes to that:

sp = SparkPost(API_KEY)
for email in emails:
    sp.transmissions.send(recipients=email, html=body, from_email=sender, subject=self.subject)
1

There are 1 best solutions below

0
Yepher On BEST ANSWER

Yes, it is best to do this in a single REST call.

By default, SparkPost REST injections are BCC and will send individual emails to each recipient. As you have seen you can also have the typical "CC" behavior but you would need to set the CC header values with the addresses you want to be seen by others.

So in the example where a CC was included you must have had something like this in the REST call:

"headers": {
    "CC": "[email protected]"
},

CC Example:

{
  "recipients": [
    {
      "address": {
        "email": "[email protected]"
      }
    },
    {
      "address": {
        "email": "[email protected]",
        "header_to": "[email protected]"
      }
    }
  ],
  "content": {
    "from": "[email protected]",
    "headers": {
      "CC": "[email protected]"
    },
    "subject": "To and CC",
    "text": "This mail was sent to [email protected] while CCing [email protected]."
  }
}

BCC Example:

"recipients": [
    {
      "address": {
        "email": "[email protected]"
      }
    },
    {
      "address": {
        "email": "[email protected]",
        "header_to": "[email protected]"
      }
    }
  ],
  "content": {
    "from": "[email protected]"
    "subject": "To and BCC",
    "text": "This mail was sent To [email protected] while BCCing an unnamed recipient. Sneaky."
  }
}

In your usecase you would not want to set "header_to": "[email protected]" for any of the recipients.