通过mailjet错误向多个收件人发送电子邮件

时间:2016-10-06 12:52:27

标签: php email mailjet

我有这个代码向多个收件人发送电子邮件,可以正常使用静态数据

$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

但是当我以这种方式使用动态数据时,我收到错误500并且没有发送电子邮件

//an array of emails from the db
$user[] = 'user1@mail.com';
$user[] = 'user2@mail.com';
foreach ($user as $email){
$to .= "[
    'Email' => '".$email."'
],";
}

并将收件人更改为:

'Recipients' => [ $to ]

当我回复$时,我得到了这个

[ 'Email' => 'user1@mail.com' ],[ 'Email' => 'user2@mail.com' ],

你能帮我在代码中找到错误吗?

1 个答案:

答案 0 :(得分:3)

$to=[];
foreach ($emails as $email){
    $to[] = [
        'Email' => $email
    ];
}

"Recipients" => $to