如何将购物车的内容通过电子邮件发送回网站管理员

时间:2018-06-05 14:04:10

标签: php html-email shopping-cart

由于我们销售的东西而不是传统的购物车结账,我们需要结帐按钮将购物车通过电子邮件发送给网络管理员,以便他批准我现在从产品数据库中抓取产品但是当它发送电子邮件,它为购物车中的每个订单项发送单独的电子邮件我希望一封电子邮件与整个购物车最有可能是一个循环问题,但似乎无法弄明白任何帮助将非常感激。

foreach($_SESSION['shoppingCart'] as $key => $product)
        {
            if (isset($_POST['checkout']))
                {
            $result = $this->qry("SELECT * FROM product WHERE Item_num=?", array($product['ID']));
            foreach ($result as $row)
            {
                echo '<tr>';
                    echo '<td>'.$row['Name'].'</td>';
                    echo '<td>'.$product['Quantity'].'</td>';
                    echo '<td>'.$row['Price'].'</td>';
                    echo '<td>$'.number_format($product['Quantity'] * $row['Price'], 2).'</td>';
                    echo '<td><form method="POST">
                        <button type="submit" name="removeFromCart" value="'.$product['ID'].'">
                        Remove</button></form></td>';
                echo '</tr>';
                $total = $total + ($product['Quantity'] * $row['Price']);
                $price= $row['Price'];
                $lprice= $product['Quantity'] * $row['Price'];
                $quan= $product['Quantity'];
                    $name = $row['Name'];
                    $quantity = $product['Quantity'];

                    $to = 'ms245@zips.uakron.edu';
                    $subject = 'order from fatfish aquatic website';





                        $message = "<html><body>";
                        $message .= "<table>";
                        $message .= "<th>Name</th><th>Quantity</th><th>Price</th> <th> line total</th>";

                        $message .= "<tr style='background: #eee;'><td>'.$name.'</td><td>'.$quan.'</td><td>'.$price.'</td><td>'.$lprice.'</td></tr>";

                        $message .="123";
                    $message .= "</table>";
                    $message .= $total;
                    $message .= $row['Name'];
                    $message .= 'shipping address <br>';
                    $message .= 'phone number <br>';


                }
                $message .= 'email<br>';
                        $message .= "</body></html>";
                        $headers  = 'MIME-Version: 1.0' . "\r\n";
                        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                        $headers .= "From: ms245@zips.uakron.edu\r\n"."X-Mailer: php";

                            mail($to, $subject, $message, $headers);





                }

            }`

1 个答案:

答案 0 :(得分:0)

试试这个,我已经重构了代码,以便在您通过购物车循环并汇总订单项详细信息后进行mail来电。我不清楚为什么你会在代码的这个时刻回应“删除产品”表格 - 如果你正在制作和发送订单详情的最终电子邮件,那么订单必须已经放好,对吧?您需要重新考虑它的这一方面,但是我已经演示了如何重构您的电子邮件构建代码以获得您所要求的内容。

if (!empty($_POST['checkout'])) {

    // initialise message
    $message = "<html><body>";
    $message .= "<table>";
    $message .= "<th>Name</th><th>Quantity</th><th>Price</th><th>line total</th>";

    foreach ($_SESSION['shoppingCart'] as $key => $product) {
        $result = $this->qry("SELECT * FROM product WHERE Item_num = ?", array($product['ID']));
        foreach ($result as $row) {
            // line item details added to message
            $name = $row['Name'];
            $quantity = $product['Quantity'];
            $price = $row['Price'];
            $lprice = $quantity * $price;
            $total += $lprice;
            $totalFormatted = number_format($total, 2);
            $message .= "<tr style='background: #eee;'><td>'.$name.'</td><td>'.$quantity.'</td><td>'.$price.'</td><td>'.$lprice.'</td></tr>";
            $message .= "123";
            $message .= $name;
        }
    }

    // finalise message
    $message .= "</table>";
    $message .= "<p id='checkout-summary'>";
    $message .= "total: &#36;{$totalFormatted} <br>";
    $message .= 'shipping address <br>';
    $message .= 'phone number <br>';
    $message .= 'email<br>';
    $message .= "</p>";
    $message .= "</body></html>";

    $to = 'ms245@zips.uakron.edu';
    $subject = 'order from fatfish aquatic website';
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: ms245@zips.uakron.edu\r\n" . "X-Mailer: php";

    mail($to, $subject, $message, $headers);

}