Send different emails to 2 different recipients with SendGrid

288 views Asked by At

I'm using this to send an email to one recipient using SendGrid, but I need to send a different email to a different recipient on the same page. If I duplicate the code block, it emails both recipients the second email when the second code block runs.

    $sendgrid = new SendGrid($sendgridapikey);
    $emailmaster  = new SendGrid\Email();
    $emailmaster->addTo($to)
    ->setFrom('<email>')
    ->setFromName('<name>')
    ->setSubject($subject)
    ->setHtml($message);
    $sendgrid->send($emailmaster);
1

There are 1 answers

2
weirdo On

Based on here explanation, you can send to multiple reception when you pass $to variable as array;

for example;

$to[] = "[email protected]";
$to[] = "[email protected]";
$sendgrid = new SendGrid($sendgridapikey);
$emailmaster  = new SendGrid\Email();
$emailmaster->addTo($to)
->setFrom('<email>')
->setFromName('<name>')
->setSubject($subject)
->setHtml($message);
$sendgrid->send($emailmaster);