开发者

How to embed html table into the body of email

I am sending info to target email via PHP native mail() method right now. Everything else works fine but the table part troubles me the开发者_运维百科 most. See sample output :

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>

The source of table is directly taken from a webpage, exactly as the same. However, Gmail, Hotmail and most of other emails will ignore to render this as a table.

So I am wondering, without using Outlook or other email sending agent software, how could I craft a embedded table for the PHP mail() method to send?

Current code snippet corresponds to table generation :

$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"));

Any hints and suggestions are welcomed, thanks a lot in advance.

Edited :

$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;
}


You can't "embed" an HTML table into plain text email. It can be only one type.
If you want to add an HTML table to your email, you have to make whole letter in HTML format and also set Content-type mail header to text/html.


As the other poster said, you'd need to send your email in HTML format, not plain text.

The PEAR Mail_Mime package makes sending HTML email pretty simple:

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


The PHP website has a section on sending mail, example #4 shows how to change your headers to send an HTML formatted email.

PHP manual on using mail

Here is the key piece of code you need...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜