如何将HTML表格嵌入到电子邮件正文中
我正在通过PHP native mail()方法发送信息到目标电子邮件。其他一切正常,但桌子部分困扰我最多。请参阅示例输出:如何将HTML表格嵌入到电子邮件正文中
Dear Michael Mao :
Thank you for purchasing flight tickets with us, here is your receipt :
Your tickets will be delivered by mail to the following address :
Street Address 1 : sdfsdafsadf sdf
Street Address 2 : N/A
City : Sydney State : nsw Postcode : 2
Country : Australia
Credit Card Number : *************1234
Your purchase details are recorded as :
<table><tr><th class="delete">del?</th><th class="from_city">from</th><th class="to_city">to</th><th class="quantity">qty</th><th class="price">unit price</th><th class="price">total price</th></tr><tr class="evenrow" id="Sydney-Lima"><td><input name="isDeleting" type="checkbox"></td><td>Sydney</td><td>Lima</td><td>1</td><td>1030.00</td><td>1030</td></tr><tr class="oddrow" id="Sydney-Perth"><td><input name="isDeleting" type="checkbox"></td><td>Sydney</td><td>Perth</td><td>3</td><td>340.00</td><td>1020</td></tr><tr class="totalprice"><td colspan="5">Grand Total Price</td><td id="grandtotal">2050</td></tr></table>
表的来源直接来自网页,完全相同。但是,Gmail,Hotmail和大多数其他电子邮件将忽略将其作为表格呈现。
所以我想知道,没有使用Outlook或其他电子邮件发送代理软件,我怎么可以为PHP邮件()方法发送一个嵌入表?
当前的代码段对应于表代:
$purchaseinfo = $_POST["purchaseinfo"];
//if html tags are not to be filtered in the body of email
$stringBuilder .= "<table>" .stripslashes($purchaseinfo) ."</table>";
//must send json response back to caller ajax request
if(mail($email, 'Your purchase information on www.hardlyworldtravel.com', $emailbody, $headers))
echo json_encode(array("feedback"=>"successful"));
else echo json_encode(array("feedback"=>"error"));
任何提示和建议表示欢迎,感谢很多提前。
编辑:
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//carft body of email to a human readable format
function buildEmailBody()
{
global $firstname, $lastname, $address1, $address2, $city, $state, $postcode, $country,
$cardnum, $email, $purchaseinfo;
$stringBuilder = ""; // * there is not such a thing as string builder in PHP
$stringBuilder .= "Dear " .$firstname ." " .$lastname ." :\n";
$stringBuilder .= "Thank you for purchasing flight tickets with us, here is your receipt :\n\n";
$stringBuilder .= "Your tickets will be deliverd by mail to the following address : \n";
$stringBuilder .= "Street Address 1 : " .$address1 ."\n";
$stringBuilder .= ($address2!="") ? "Street Address 2 : " .$address2 ."\n" : "Street Address 2 : N/A\n";
$stringBuilder .= "City : " .$city;
$stringBuilder .= ($state!="") ? "State : " .$state : "State : N/A ";
$stringBuilder .= "Postcode : " .$postcode ."\n";
$stringBuilder .= "Country : " .$country ."\n";
$stringBuilder .= "Credit Card Number : " .$cardnum ."\n\n";
$stringBuilder .= "Your purchase details are recorded as : \n\n";
//if html tags are not to be filtered in the body of email
$stringBuilder .= "<table>" .stripslashes($purchaseinfo) ."</table>";
//otherwise this will be painful to retrieve info out of the html table.
return $stringBuilder;
}
PHP的网站有关于发送邮件部分,例如#4显示了如何改变你的头发送HTML格式的电子邮件。
下面是你需要的代码关键部分...
// 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";
@Zachary:谢谢你指出这一点。我一直在忘记我正在处理纯文本,而不是另一个html页面...... – 2010-04-01 06:08:26
你不能 “嵌入” 的HTML表到纯文本电子邮件。它只能是一种类型。
如果您想要将HTML表格添加到您的电子邮件中,则必须以HTML格式制作整个字母,并将Content-type
邮件标题设置为text/html
。
正如另一位海报所说,您需要以HTML格式发送您的电子邮件,而不是纯文本。
的PEAR Mail_Mime封装使发送HTML邮件很简单:
@ericofsac:感谢您的提示!对于uni任务标记的缘故,我只是懒得把自己手上的所有东西都制作出来:) – 2010-04-01 06:09:20
你能告诉在表格为实际投入的电子邮件($ emailbody)? – 2010-04-01 05:49:29
@Pekka:对不起,忘了添加更多的代码,我会更新这个问题。 – 2010-04-01 05:53:52