在电子邮件正文中插入表格

时间:2014-08-07 22:46:21

标签: php html

我编写了下面的代码,在电子邮件中插入表格并发送给它。当表包含1或2个项目时,它们将在2行显示良好但如果有超过2个项目,则剩余项目将显示在第二行之后但完全在表格的左侧。 我不知道这里有什么问题。谢谢你的帮助

<?php
$to = "$user->email";
$from = "sales@exemple.com"; //the email address from 
$headers  = "From: ".$from."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "Your order";
$message = "<html>
        <body>
            <p>Order Details test</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:20%; border-bottom:solid 1px #000\">Quantity</th>
                    <th style=\"width:20%; border-bottom:solid 1px #000\">Unit Price</th>
                </tr>
                <tr>";

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

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

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

if ($mailsent) {
//echo "<p>Yay! The following data has been sent:<br /><br />";
//echo "<strong>By: </strong> " . $from . "<br />";
//echo "<strong>About:</strong> " . $subject . "<br />";
//echo "<strong>Comments:</strong>" . $message . "<br />";
//echo "</p>";
}
else {
echo "There was an error sending your order confirmation";
}


?>

1 个答案:

答案 0 :(得分:2)

您的<tr>必须分别为:

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