PHP电子邮件没有回显foreach循环

时间:2015-06-20 11:50:16

标签: php email foreach

我试图发送一封php电子邮件,其中列出了客户订购的所有产品。起初我只是列出了数量,产品,细节等变量。它有效,但只有一个产品被包含在电子邮件中。所以我认为需要一个foreach循环。电子邮件中的所有内容都正常工作,但我的foreach循环并未回显产品详细信息。

此行没有回显......

 echo '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';



$to = $email;
$subject = 'Your Example order';
$message = '<img src="'.$logoImage.'">';
$message .= '
<html>
<head>
  <title>Example Order</title>
</head>
<body>
    <p>Hi '.$billToName.',</p><br><br>
    <p>Thank you for ordering with us!</p>
      <p>Your order was successfully sent to us and we will start processing it immediately.</p><br><br>
      <p>A charge of $'.$total_price.' was placed on '.$billToName.'\'s card that was used for the order.
      <p>Your order contained:</p>';
 foreach($_SESSION['shopping_cart'] as $id => $product) {
        $product_id = $product['product_id'];
        $prdname =  $products[$product_id]['product_name'];
        $prdprice =  $products[$product_id]['price'];
        $prdqty = $product['quantity'];
        $prdsize = $product['size'];
        echo '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';                
        }

$message .='

      <p>'.  $AuthorrizeResponseText.';</p><br><br>
      <p>We really appreciate your business!</p>
</body>
</html>
';
$from = "auto-confirm@example.com";
$cc = "order-receipts@example.com";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";

// Send the email
mail($to,$subject,$message,$headers);

任何人都会看到任何阻止这种情况的事情吗?

4 个答案:

答案 0 :(得分:1)

您需要将以下行替换为foreach循环

echo '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';

$message .= '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';

答案 1 :(得分:1)

这是回声,但我认为你想要将产品连接到消息,所以:

echo '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';

应该是:

$message .= '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';

答案 2 :(得分:1)

而不是

echo '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';  

使用

$message .= '<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';  

答案 3 :(得分:0)

你没有在$ message变量

中连接foreach echo语句

你的$ message应该是这样的

$message .= '
<html>
<head>
  <title>Example Order</title>
</head>
<body>
    <p>Hi '.$billToName.',</p><br><br>
    <p>Thank you for ordering with us!</p>
      <p>Your order was successfully sent to us and we will start processing it immediately.</p><br><br>
      <p>A charge of $'.$total_price.' was placed on '.$billToName.'\'s card that was used for the order.
      <p>Your order contained:</p>';
 foreach($_SESSION['shopping_cart'] as $id => $product) {
        $product_id = $product['product_id'];
        $prdname =  $products[$product_id]['product_name'];
        $prdprice =  $products[$product_id]['price'];
        $prdqty = $product['quantity'];
        $prdsize = $product['size'];
        $message .='<p>'.$prdqty.' - '.$prdsize.' - '.$prdname.'</p><br><br>';               
        }

$message .='

      <p>'.  $AuthorrizeResponseText.';</p><br><br>
      <p>We really appreciate your business!</p>
</body>
</html>
';