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)
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": "cc@thatperson.com"
},
{
"recipients": [
{
"address": {
"email": "to@thisperson.com"
}
},
{
"address": {
"email": "cc@thatperson.com",
"header_to": "to@thisperson.com"
}
}
],
"content": {
"from": "you@fromyou.com",
"headers": {
"CC": "cc@thatperson.com"
},
"subject": "To and CC",
"text": "This mail was sent to to@thisperson.com while CCing cc@thatperson.com."
}
}
"recipients": [
{
"address": {
"email": "to@thisperson.com"
}
},
{
"address": {
"email": "bcc@thatperson.com",
"header_to": "to@thisperson.com"
}
}
],
"content": {
"from": "you@fromyou.com"
"subject": "To and BCC",
"text": "This mail was sent To to@thisperson.com while BCCing an unnamed recipient. Sneaky."
}
}
In your usecase you would not want to set "header_to": "to@thisperson.com"
for any of the recipients.