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

308
Views
Sendgrid php envía a múltiples destinatarios

Tengo un script php de sendgrid simple para enviar correos electrónicos, el único problema aquí es que necesito agregar más destinatarios, por lo que este código funciona solo para un destinatario, estaba mirando la documentación oficial pero no pude encontrar ninguna información útil, ¿hay alguien que sabe cómo y qué necesito cambiar aquí para agregar más destinatarios/correos electrónicos.

 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

La clase SendGrid\Mail admite la adición de varias direcciones to través de la clase SendGrid\Personalization .

Puede ver un ejemplo aquí: https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35

Piense en una Personalization como el sobre de su correo electrónico. Contiene las direcciones de los destinatarios y otros datos similares. Cada objeto Sendgrid\Mail debe tener al menos una Personalization .

A través del constructor que está utilizando, ya se creó un objeto de Personalization para usted, consulte aquí: https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958

Puede crear un objeto de Mail sin esto y luego agregar su propia Personalization .

about 4 years ago · Santiago Trujillo Report

0

Al final, así es como he logrado hacer esto y está funcionando bien.

 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!