'SendGrid: Sending an email to multiple recipients without other emails being shown on the "to" field
I want to send an email to multiple recipients.
I used personalizations, but everyone's emails appears on the "to" field, which violates their privacy.
I don't want to use BCC as this often goes straight to junk (e.g. http://www.standss.com/blog/index.php/why-you-should-avoid-using-bcc-for-emails/).
Therefore my question is, how can I send an email to multiple recipients without everyones emails appearing on the "to" field.
My only alternative I can see is send a separate request to the API for each and every email using a loop which is very resource intensive and time consuming when I have a lot of emails to send!
Solution 1:[1]
When using SendGrid's Personalizations with multiple recipient groups, you need to define multiple 1st-level objects within the Personalization array.
So instead of:
{"personalizations": [
{"to": [
{"email": "[email protected]"},
{"email": "[email protected]"}
]}]}
which will all be a common To:
array that can see each other,
You want:
{"personalizations": [
{"to": [{"email": "[email protected]"}]},
{"to": [{"email": "[email protected]"}]}
]}
Within each personalization level, you can customize the content, subject, substitution tags, pretty much everything.
So you can build out the full personalization, and iterate through those 1000 times. Once you have your 1000 recipients, bundle them into a single API call, then send that.
Solution 2:[2]
Here's a C# version that clones Personalization for each recipient to be mailed separately, but still with a single SendGrid API call:
public static void SendEachReceipient(SendGridMessage msg, IEnumerable<string> recipients)
{
if (msg == null || recipients == null || !recipients.Any())
return;
if (msg.Personalizations == null) //can easily be null if no substitutions have not been added
msg.Personalizations = new List<Personalization>();
var substitutionsCopy = msg.Personalizations.FirstOrDefault()?.Substitutions; //all substitutions (if any) are always all contained in the first personalization
msg.Personalizations.Clear(); //we will start fresh - one personalization per each receipient to keep emails private from each other
foreach (var email in recipients.Where(x => !string.IsNullOrEmpty(x)).Distinct())
{
var personalization = new Personalization();
personalization.Substitutions = substitutionsCopy;
personalization.Tos = new List<EmailAddress>() { new EmailAddress(email) };
msg.Personalizations.Add(personalization);
}
var result = new SendGridClient("api-key").SendEmailAsync(msg).Result;
}
Solution 3:[3]
To build on @jacobmovingfwd, here's an example in Python that sends the same email to multiple recipients with individualized "to" fields. I've tested the code, and it works for me.
# Given a list of email addresses that are strings
sublist = [...]
mail = Mail()
for to_email in sublist:
# Create new instance for each email
personalization = Personalization()
# Add email addresses to personalization instance
personalization.add_to(Email(to_email))
# Add personalization instance to Mail object
mail.add_personalization(personalization)
# Add data that is common to all personalizations
mail.from_email = Email(from_email)
mail.subject = subject
mail.add_content(Content('text/plain', message_txt))
mail.add_content(Content('text/html', message_html))
# Send
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
response = sg.client.mail.send.post(request_body=mail.get())
Solution 4:[4]
You Need to prepare the below type of JSON
{
"personalizations": [
{
"to": [
{
"email": "mail here",
"name": "name here"
}
],
"subject": "subject for individual person"
},
{
"to": [
{
"email": "mail here",
"name": "name here"
}
],
"subject": "if you want to send a dynamic subject then write here",
}
],
"from": {
"email": "mail here",
"name": "name here"
},
"reply_to": {
"email": "mail here",
"name": "name here"
},
"subject": "subject here",
"content": [
{
"type": "text/html",
"value": "<p>Hello from Twilio first!</p>"
}
],
"attachments": [
{
"content": "PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ImVuIj4KCiAgICA8aGVhZD4KICAgICAgICA8bWV0YSBjaGFyc2V0PSJVVEYtOCI+CiAgICAgICAgPG1ldGEgaHR0cC1lcXVpdj0iWC1VQS1Db21wYXRpYmxlIiBjb250ZW50PSJJRT1lZGdlIj4KICAgICAgICA8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLCBpbml0aWFsLXNjYWxlPTEuMCI+CiAgICAgICAgPHRpdGxlPkRvY3VtZW50PC90aXRsZT4KICAgIDwvaGVhZD4KCiAgICA8Ym9keT4KCiAgICA8L2JvZHk+Cgo8L2h0bWw+Cg==",
"filename": "index.html",
"type": "text/html",
"disposition": "attachment"
}
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jacobmovingfwd |
Solution 2 | rothschild86 |
Solution 3 | user7355302 |
Solution 4 | Darshan Singh |