0

I'm trying to send messages to multiple recipients with different message body but I don't know if my script has any error. The problem is when I execute this below code, only one user email will receive message. If I try it again another one will get a message. I want all the emails in the array to receive the message with individual message body. And also I noticed that my script takes long to complete execution. Is there a better way to get this working as expected?

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;
}
xanadev
  • 751
  • 9
  • 26
Peter
  • 1,860
  • 2
  • 18
  • 47
  • It's hard to find an error - try to add some debug output, maybe there are not all expected emails in `email_list` - print them out `print_r($email_list); ` before you iterate through the emails to check that. – Evil_skunk Jun 03 '18 at 15:44
  • 1
    And sending emails could take some time - this use case sounds more like an background job which should be executed directly on the server instead through a user web-request (see for example https://stackoverflow.com/questions/4626860/how-can-i-run-a-php-script-in-the-background-after-a-form-is-submitted) – Evil_skunk Jun 03 '18 at 15:45
  • @Evil_skunk am trying to use your solution but am have problem with the shell_exec, i never use that before – Peter Jun 04 '18 at 18:45
  • basically your normal script (but only without references to `$_SESSION` or `$_GET` / `$_POST`) can be runned via shell_exec - I can help you, but I need Info what you have already tried and your current code and possible error messages – Evil_skunk Jun 04 '18 at 19:49
  • @Evil_skunk i will purchase a VPS, but am scared because of security i never use it before and i don't have any server administrator to help me secure it. But from my hosting provider that is the only option they provide – Peter Jun 04 '18 at 20:38

0 Answers0