I'm having this code to send an email to multiple recipients which works fine with static data
$subject = 'test';
include("mailjet/php-mailjet-v3.class.php");
$mj = new Mailjet( 'api', 'secret' );
$params = array(
"method" => "POST",
"FromEmail" => "order@domain.com",
"FromName" => "sender name",
"Subject" => "subject here",
"Html-Part" => "<html><table><tr><td colspan='2'>message</td></tr></table></html>",
"Text-Part" => "message",
'Recipients' => [
[
'Email' => "passenger1@mailjet.com",
'Name' => "passenger 1"
],
[
'Email' => "passenger2@mailjet.com",
'Name' => "passenger 2"
]
]
);
$result = $mj->send($params);
echo $mj->_response_code; //200 success
but when I use dynamic data in this way i get error 500 and email wasn't sent
//an array of emails from the db
$user[] = 'user1@mail.com';
$user[] = 'user2@mail.com';
foreach ($user as $email){
$to .= "[
'Email' => '".$email."'
],";
}
and change the Recipients to:
'Recipients' => [ $to ]
when I echo $to I get this
[ 'Email' => 'user1@mail.com' ],[ 'Email' => 'user2@mail.com' ],
Can you help me find the error in my code?