如何将html表嵌入到电子邮件正文中

时间:2010-04-01 05:38:50

标签: php

我现在正通过PHP native mail()方法向目标电子邮件发送信息。其他一切都很好,但桌子部分最让我烦恼。见样本输出:

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 mail()方法制作嵌入式表格?

当前代码段对应于表格生成:

$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: fake@hardlyworldtravel.com' . "\r\n" .
        'Reply-To: fake@hardlyworldtravel.com' . "\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;
}

3 个答案:

答案 0 :(得分:1)

您无法将 HTML 表格“嵌入”纯文本电子邮件中。它只能是一种类型 如果要在电子邮件中添加HTML表格,则必须以HTML格式制作整个字母,并将Content-type邮件标题设置为text/html

答案 1 :(得分:1)

正如另一张海报所说,你需要以HTML格式发送电子邮件,而不是纯文本。

PEAR Mail_Mime包使发送HTML电子邮件变得非常简单:

http://pear.php.net/package/Mail_Mime/

答案 2 :(得分:0)

PHP网站有一个关于发送邮件的部分,示例#4显示了如何更改标题以发送HTML格式的电子邮件。

PHP manual on using mail

以下是您需要的关键代码......

// 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";