如何在电子邮件中嵌入带有PHP数组的HTML?

时间:2014-05-07 20:28:23

标签: php jquery html

我试图在访客提交订单后发送订单详细信息。我试图将我的订单数组结果嵌入到html标记中,但它无法正常工作。请帮忙

senmail.php

<?php

$to = "member@yahoo.com";
$from = "sales@thesite.com"; 
$headers = "From: $from";
$subject = "Custom computer Order Confirmation Nunber_ $orderid";
$message = " 
 <html>
 <body>
   <p> This doesn't render well</p> 
   <table style="margin-left:50px; text-align:left;">
     <tr>
       <th style="width:20%; border-bottom:solid 1px #000;">Item ID</th>
       <th style="width:40%; border-bottom:solid 1px #000">Description</th>
       <th style="width:15%; border-bottom:solid 1px #000">Quantity</th>
       <th style="width:20%; border-bottom:solid 1px #000">Unit Price</th>
     </tr>  

   <?php 
     foreach($_SESSION["cart_array"]as $item): 
        $item_id=$item['part_id'];
        $sql = mysqli_query($con,"SELECT * FROM   product_description        
        WHERE product_id='$item_id' LIMIT 1");

        While($row=mysqli_fetch_array($sql)){
        $product_name=$row["name"];      
     }   
   ?>

  <tr>
    <td><?php echo $item['part_id'] ?></td>
    <td><?php echo $product_name ?></td>
    <td><?php echo $item['quantity'] ?></td>     
    <td><?php echo $item['price'] ?></td>
  </tr>
   <?php endforeach; ?>
 </table>
</body>
</html> 
 ";
 $mailsent = mail($to, $subject, $message, $headers);

.........

?>

3 个答案:

答案 0 :(得分:2)

无聊到足以为你解决这个问题:

<?php

$to = "member@yahoo.com";
$from = "sales@thesite.com";
$headers = "From: $from";
$subject = "Custom computer Order Confirmation Nunber_ $orderid";
$message = '
 <html>
 <body>
   <p> This doesn\'t render well</p>
   <table style="margin-left:50px; text-align:left;">
     <tr>
       <th style="width:20%; border-bottom:solid 1px #000;">Item ID</th>
       <th style="width:40%; border-bottom:solid 1px #000">Description</th>
       <th style="width:15%; border-bottom:solid 1px #000">Quantity</th>
       <th style="width:20%; border-bottom:solid 1px #000">Unit Price</th>
     </tr>';


foreach($_SESSION["cart_array"] as $item){
    $item_id = $item['part_id'];
    $sql = mysqli_query($con,"SELECT * FROM   product_description
        WHERE product_id='$item_id' LIMIT 1");

    While ( $row = mysqli_fetch_array($sql) ){
        $product_name = $row["name"];
    }

    $message .= '
  <tr>
    <td>'.$item['part_id'].'</td>
    <td>'.$product_name.'</td>
    <td>'.$item['quantity'].'</td>
    <td>'.$item['price'].'</td>
  </tr>';
}

$message .= '  </table>
</body>
</html>';


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

//.........


?>

我只修复了基础知识,这仍然远非理想

答案 1 :(得分:0)

你不能在代码中嵌入PHP代码,并且神奇地让它执行。

正确的方法是将消息存储在字符串中并使用处理循环添加到其中。这样的结果看起来像:

$msg = "Beginning of message\n";
foreach($_SESSION["cart_array"]as $item) {
    $item_id=$item['part_id'];
    $sql = mysqli_query($con,"SELECT * FROM   product_description        
    WHERE product_id='$item_id' LIMIT 1");

    while ($row = mysqli_fetch_array($sql)) {
        $product_name = $row["name"];
        $msg .= "<td>$product_name</td>";
        // Other message parts go here
    } 
$msg .= "End of message\n";

或者,你可以走一条非常可怕的路线并使用类似eval的东西,但因为我不鼓励这种行为,所以我不会提供一个例子。

答案 2 :(得分:-2)

您无法转发$message中的引号。使用'\"代替html。此外,您还不需要$message中间的额外PHP标记,因为您仍在使用PHP编写。在foreach内联$message .=内,最后它将是一个字符串,您可以通过电子邮件提交。我还没有对其进行测试,但我清理了您的代码并在下面进行了建议的更改。

$message = " 
    <html>
        <body>
            <p> This doesn't render well</p> 
            <table style="margin-left:50px; text-align:left;">
                <tr>
                ...

应该是

$message = " 
    <html>
        <body>
            <p> This doesn't render well</p> 
            <table style=\"margin-left:50px; text-align:left;\">
                <tr>
                ...

你去吧

<?php

$to = "member@yahoo.com";
$from = "sales@thesite.com"; 
$headers = "From: $from";
$subject = "Custom computer Order Confirmation Nunber_ $orderid";
$message = "<html>
            <body>
                <p>This doesn't render well</p>
                <table style=\"margin-left:50px; text-align:left;\">
                    <tr>
                        <th style=\"width:20%; border-bottom:solid 1px #000\">Item ID</th>
                        <th style=\"width:40%; border-bottom:solid 1px #000\">Description</th>
                        <th style=\"width:15%; border-bottom:solid 1px #000\">Quantity</th>
                        <th style=\"width:20%; border-bottom:solid 1px #000\">Unit Price</th>
                    </tr>";

foreach( $_SESSION["cart_array"] as $item )
{
    $item_id = $item["part_id"];
    $sql     = mysqli_query( $con, "SELECT * FROM product_decsription WHERE product_id='".$item_id."' LIMIT 1;" );

    if($row=mysqli_fetch_array($sql))
    {
        $message .= "
                    <tr><td>".$item['part_id']."</td>
                        <td>".$row['name']."</td>
                        <td>".$item['quantity']."</td>     
                        <td>".$item['price']."</td></tr>";
    }
}

$message .= "</table></body></html>";
$mailsent = mail( $to, $subject, $message, $headers );

?>

编辑:要使用html的标头需要:

$headers  = "From: ".$from."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";