PHP在电子邮件中发送循环

时间:2016-01-11 09:35:27

标签: php arrays email loops

这是我的购物车循环, 我如何将其作为数组发送到我的电子邮件正文中?我可以创建一个函数或者必须是数组中的循环吗? 谢谢你的帮助。

  if(isset($_SESSION["cart_products"]))
{
    $total = 0; //set initial total value
    $b = 0; //var for zebra stripe table 
    foreach ($_SESSION["cart_products"] as $cart_itm)
    {
        //set variables to use in content below
        $product_name = $cart_itm["product_name"];
        $product_qty = $cart_itm["product_qty"];
        $product_price = $cart_itm["product_price"];
        $product_code = $cart_itm["product_code"];
        $product_color = $cart_itm["product_color"];
        $subtotal = ($product_price * $product_qty); //calculate Price x Qty

        $bg_color = ($b++%2==1) ? 'odd' : 'even'; //class for zebra stripe 
        echo '<tr class="'.$bg_color.'">';
        echo '<td><input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
        echo '<td>'.$product_name.'</td>';
        echo '<td>'.$product_price.$currency.'</td>';
        echo '<td>'.$subtotal.$currency.'</td>';
        echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /></td>';
        echo '</tr>';
        $total = ($total + $subtotal); //add subtotal to total var
    }

    $grand_total = $total + $shipping_cost; //grand total including shipping cost


}

2 个答案:

答案 0 :(得分:0)

通过这种方式:

$to = "test@test.com";
$subject= "test";
mail($to,$subject,print_r($yourArr,true));

<强>解释

使用true作为print_r()函数中的第二个参数,并将其传递给邮件正文参数。

答案 1 :(得分:0)

我是这样做的:在send.php中创建了一个循环

foreach ( $_SESSION["cart_products"] as $cart_itm ) {
$txt .= " <tr><td style=\"border: 1px solid black; width:200px;\">";
$txt .= $cart_itm["product_name"];
$txt .= "</td><td style=\"border: 1px solid black\">";
$txt .= $cart_itm["product_qty"];
$txt .= "</td><td style=\"border: 1px solid black\">";
$txt .= $cart_itm["product_price"] .$currency;
$txt .= "</td><td style=\"border: 1px solid black\">";
$txt .= ($cart_itm["product_price"] * $cart_itm["product_qty"]) .$currency ;
$txt .= "</td></tr> ";
$subtotal = ($cart_itm["product_price"] * $cart_itm["product_qty"]);
$total = ($total + $subtotal);
}

比在电子邮件正文中:

Products:  
<br> 
<table td style=\"border: 1px solid black;text-align:left;border-collapse: collapse;margin:15px;\">
                        <tr>
                            <td>Product:    </td>
                            <td>Q-ty        </td>
                            <td>Price       </td>
                            <td>Total       </td>
                        </tr>
                        ".$txt."
                        <tr style=\"text-align:right\">
                            <td colspan=\"4\">
                            <br>
                            <b>Grand total:".$total." ".$currency." </b>
                            </td>
                        </tr>   
                </table>

因此循环为每个产品创建一行并将其插入表格行。 其余的是在电子邮件中形成表格的样式