2010-04-01 171 views
0

我正在通過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; 
} 
+0

你能告訴在表格爲實際投入的電子郵件($ emailbody)? – 2010-04-01 05:49:29

+0

@Pekka:對不起,忘了添加更多的代碼,我會更新這個問題。 – 2010-04-01 05:53:52

回答

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"; 
+0

@Zachary:謝謝你指出這一點。我一直在忘記我正在處理純文本,而不是另一個html頁面...... – 2010-04-01 06:08:26

1

你不能 「嵌入」 的HTML表到純文本電子郵件。它只能是一種類型。
如果您想要將HTML表格添加到您的電子郵件中,則必須以HTML格式製作整個字母,並將Content-type郵件標題設置爲text/html

1

正如另一位海報所說,您需要以HTML格式發送您的電子郵件,而不是純文本。

的PEAR Mail_Mime封裝使發送HTML郵件很簡單:

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

+0

@ericofsac:感謝您的提示!對於uni任務標記的緣故,我只是懶得把自己手上的所有東西都製作出來:) – 2010-04-01 06:09:20

相關問題