Skip to content
Advertisement

Load dynamic values into msgHTML in phpmailer body from database

What I am trying doing here is inserting and sending the number of orders via SMTP phpmailer. I am using foreach to separate the order data into key value pair and inserting into the database.

can see here: here

once the execution is successful now I want to send a mail to the owner with the order details which is just inserted. Just for a test I only picked productid as a order detail.

Here is the mail that just arrived. here

Problem

What I am looking is the database has inserted 2 productid (26,27) but in mail i only got productid (26) I want the dynamic productid insertion inside the mail body. I use while loop but I thing the goal cannot achieve or fulfill. I would be appreciate if anyone printout where I am getting wrong.

PHP:

$shown = false;
        foreach($_SESSION["shopping_cart"] as $number => $val)
            {
                       // prepare and bind
                        $stmt = $conn->prepare("INSERT INTO purchase(guest_code,productid,quantity,date_purchase) VALUES (?, ?, ?, ?)");
                        $stmt->bind_param("siis",$_SESSION['CODE'],$val['product_id'],$val['product_quantity'],$current_date_time); //inserting mutiple value in db successful.

                        if($stmt->execute())
                        {
                            if(!$shown) //show msg or dedirect only once
                            {      

                                    $sql= mysqli_query($conn,"select productid from purchase where guest_code='".$_SESSION['CODE']."'"); //fetch only productid for test
                                    while ($row = mysqli_fetch_array($sql)) 
                                    {
                                          $productid = $row['productid'];
                                          $message = "<tr><td style='text-align:center;'><strong>".$productid."</td></tr>";
                                          $mail->msgHTML($message); //trying to load multiple productid in $message 
                                    }    

                                        $mail->Subject = 'New Order Arrived!';
                                        if(!$mail->send()) 
                                        {
                                          $mail_error = 'error: ' . $mail->ErrorInfo;
                                          exit();
                                        } 
                                        else {
                                               //if mail sent then
                                                unset($_SESSION["shopping_cart"]); //distroy all the values in the cart
                                                //header('location:../checkout.php?er=false');
                                        }


                                $shown = true;

                            }


                        }else
                        {

                            if(!$shown) //show msg only once
                            {
                                echo 'ERROR: while placing your order. Please contact restaurant owner.';
                                $shown = true;
                            }
                        }


            }

Advertisement

Answer

Every time you call $mail->msgHTML($message); it replaces the entire contents of Body; it doesn’t add to it. I would expect you do something along the lines of:

$mail->Body = '<p>Your order</p><table>';
while ($row = mysqli_fetch_array($sql)) {
    $productid = $row['productid'];
    $mail->Body .= "<tr><td style='text-align:center;'><strong>".$productid."</td></tr>";
}
$mail->Body .= '</table>';
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement