Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda