Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
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, "sample@email.com");
    $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();
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

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, "sample@email.com");
    $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();
 }
about 4 years ago · Santiago Trujillo Report

0

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('test1@example.com','test2@example.com');
$from = 'from@example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";

sendMail($to ,$from,$subject,$body);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!