Php代码向不同邮件正文的多个收件人发送邮件

时间:2018-06-03 12:52:22

标签: php arrays loops email

我正在尝试向具有不同邮件正文的多个收件人发送邮件,但我不知道我的脚本是否有任何错误。问题是当我执行下面的代码时,只有一个用户电子邮件会收到消息。如果我再试一次,另一个人会得到一条消息。我希望数组中的所有电子邮件都能收到带有单个邮件正文的邮件。而且我注意到我的脚本需要很长时间才能完成执行。是否有更好的方法让它按预期工作?

PHP

<?php 

$conn_handler->prepare('
    SELECT * FROM food_orders fo
    INNER JOIN our_chefs oc
    ON oc.chef_private_key = fo.order_chefpkey
    WHERE fo.order_id = :currentorder AND fo.order_userid = :order_userid
    ORDER BY fo.order_chefpkey
');

$conn_handler->bind(':currentorder', $lastOrderId);
$conn_handler->bind(':order_userid', $buyerid);
$conn_handler->execute();
$getFoodOrders = $conn_handler->getAll();

if( !isset($_SESSION['completed_'.$lastOrderId]) ) {
    $creatProducts = array();
    $email_list = array();
    /*Here i loop on current orders*/
    foreach($getFoodOrders as $row) {
        //Create an array of chef emails
        $email_list[$row->chef_private_key] = $row->chef_email; 

        //Create an array of items based on chef private key
        $creatProducts[$row->order_chefpkey][] = array( 
            'o_name' => $row->order_foodname,
            'o_pid' => $row->oder_foodid,
            'o_price' => $row->order_price,
            'o_currency' => $row->currency,
            'o_qty' => $row->order_qty,
            'o_size' => $row->order_size,
            'o_img' => $row->order_image,
        );
     }

    //Here i loop through the above chef emails
    foreach($email_list as $key => $val) {
        $productBuilder =  null;
        //Here i create html for products based on chef keys
        foreach($creatProducts[$key] as $erow) { 
            $productBuilder .= '<div><b>Product Name:</b> '.$erow['o_name'].'<br/></div>';
        }
        //Here i send email to each chef with their individual products created above
        $sourcejail->sendMail(
             $val, //Send TO
             null, //Send Bcc
             null, //Send CC
             null, //reply To
             1,    //Something
             'Your have received new order ('.$lastOrderId.')', //Subject
             $productBuilder //Message body
        );
    }
    $_SESSION['completed_'.$lastOrderId] = true;
}

0 个答案:

没有答案