Sahbaz
Sahbaz

Reputation: 1272

Sendgrid php send to multiple recipients

I have simple sendgrid php script to send email, only issue here is that i need to add more recipients, so this code works only for one recipient, i was looking at official documentation but was unable to find any useful info, is there anyone who knows how and what i need to change here to add more recipients/emails.

function sendEmail($subject, $to, $message) {
    $from = new SendGrid\Email(null, "[email protected]");
    $subject = $subject;

    $to = new SendGrid\Email(null, $to);
    $content = new SendGrid\Content("text/html", $message);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);

    $apiKey = 'MY_KEY';
    $sg = new \SendGrid($apiKey);

    $response = $sg->client->mail()->send()->post($mail);
    echo $response->statusCode();
}

Upvotes: 5

Views: 14498

Answers (5)

Omair Munir
Omair Munir

Reputation: 143

If someone is still looking for an answer to how to add multiple emails in To, Cc, and Bcc, Using SendGrid, here is what helped me.

First, you need to add an associative array like this: for multiple emails in to (recipients):

$tos = [
"[email protected]" => "User 1", 
"[email protected]" => "User 2"
];

and in your SendMail class use this $email->addTos($tos); instead of $email->addTo;

Similarly, for multiple Cc use

$email->addCcs($cc);

and for Bcc

$email->addBccs($bcc);

Here is the link to see more details sendgrid php

Upvotes: 2

Sahbaz
Sahbaz

Reputation: 1272

In the end, this is how I have managed to do this and it's working good.

function sendEmail($subject, $to, $message, $cc)
{
    $from = new SendGrid\Email(null, "[email protected]");
    $subject = $subject;

    $to = new SendGrid\Email(null, $to);
    $content = new SendGrid\Content("text/html", $message);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);

    foreach ($cc as $value) {
        $to = new SendGrid\Email(null, $value);
        $mail->personalization[0]->addCC($to);
    }

    $apiKey = 'MY_KEY';
    $sg = new \SendGrid($apiKey);

    $response = $sg->client->mail()->send()->post($mail);
    echo $response->statusCode();
 }

Upvotes: 3

Haritsinh Gohil
Haritsinh Gohil

Reputation: 6272

Now Sendgrid provides an easy way for sending single mail to multiple recipients,

it provides Mail::addTos method in which we can add multiple mails to whom we want to send our mail,

we have to pass associative array of user emails and user names into addTos.

see below example:

$tos = [ 
        //user emails       =>  user names  
        "[email protected]" => "Example User1",
        "[email protected]" => "Example User2",
        "[email protected]" => "Example User3"
    ];
    $email->addTos($tos);

If you want to see Full example which is provided in sendgrid-php github library then i have included it below, so you can understand the whole example:

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("[email protected]", "Example User");
$tos = [ 
    "[email protected]" => "Example User1",
    "[email protected]" => "Example User2",
    "[email protected]" => "Example User3"
];
$email->addTos($tos);
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

Ref: https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md#send-an-email-to-multiple-recipients

Upvotes: 0

Nanhe Kumar
Nanhe Kumar

Reputation: 16307

function makeEmail($to_emails = array(),$from_email,$subject,$body) {
    $from = new SendGrid\Email(null, $from_email);

    $to = new SendGrid\Email(null, $to_emails[0]);
    $content = new SendGrid\Content("text/plain", $body);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);
    $to = new SendGrid\Email(null, $to_emails[1]);
    $mail->personalization[0]->addTo($to);

    return $mail;
}

function sendMail($to = array(),$from,$subject,$body) {

    $apiKey = 'your api key';
    $sg = new \SendGrid($apiKey);
    $request_body = makeEmail($to ,$from,$subject,$body);
    $response = $sg->client->mail()->send()->post($request_body);
    echo $response->statusCode();
    echo $response->body();
    print_r($response->headers());
}

$to = array('[email protected]','[email protected]');
$from = '[email protected]';
$subject = "Test Email Subject";
$body = "Send Multiple Person";

sendMail($to ,$from,$subject,$body);

Upvotes: 1

The SendGrid\Mail class supports adding multiple to addresses through the SendGrid\Personalization class.

You can see an example here: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35

Think of a Personalization as the envelope for your email. It holds the recipient's addresses and other similar data. Each Sendgrid\Mail object, must have at least one Personalization.

Through the constructor you are using, a Personalization object is already created for you, see here: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958

You can create a Mail object without this and later add your own Personalization.

Upvotes: 6

Related Questions