SendGrid: Sending an email to multiple recipients without other emails being shown on the "to" field



PHP Snippet 1:

{"personalizations": [
{"to": [
    {"email": "[email protected]"},
    {"email": "[email protected]"}
]}]}

PHP Snippet 2:

{"personalizations": [
{"to": [{"email": "[email protected]"}]},
{"to": [{"email": "[email protected]"}]}
]}

PHP Snippet 3:

    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;
    }

PHP Snippet 4:

# 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())

PHP Snippet 5:

{
"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"
    }
] 
}