2012-12-06 54 views
0

我遇到了一個問題,當我使用mail()發送郵件時,它會在郵件中發送HTML代碼。這是我的代碼:PHP郵件()函數,以HTML代碼發送郵件

<?php 
$message = "<table width='100%' border='0' cellspacing='0' cellpadding='3'> 
    <tr> 
    <td colspan=2>Dear ".$_POST['fname']."&nbsp;".$_POST['lname'].",</td> 
    </tr> 
    <tr> 
    <td colspan=2>Thank you for contacting us.We will get back to you in next 48 hrs.Your Contacts Details are as follows:</td> 
    </tr> 
    <tr> 
    <td width='28%' >Title</td> 
    <td width='72%'>".$_POST['title']."</td> 
    </tr> 
    <tr> 
    <td>Email</td> 
    <td>".$_POST['email']."</td> 
    </tr> 
    <tr> 
    <td>Your Message</td> 
    <td>".$_POST['message']."</td> 
    </tr> 
    <tr> 
    <td>How did you hear about us? </td> 
    <td>".$checkBox."</td> 
    </tr> 
</table> 
"; 

$subject = "ContactUs"; 
$headers .= "From: ".$from_mail." \r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 

mail($_POST["coach"], $subject, $message, $headers); 

?> 

和我在電子郵件我得到的輸出是這樣的:

<table width='100%' border='0' cellspacing='0' cellpadding='3'> 
    <tr> 
    <td colspan=2>Dear Test User,</td> 
    </tr> 
    <tr> 
    <td colspan=2>Thank you for contacting us.Your Contacts Details are as follows:</td> 
    </tr> 
    <tr> 
    <td width='28%' >Title</td> 
    <td width='72%'>Mr.</td> 
    </tr> 
    <tr> 
    <td>Email</td> 
    <td>[email protected]</td> 
    </tr> 
    <tr> 
    <td>Your Message</td> 
    <td>Test Message</td> 
    </tr> 
    <tr> 
    <td>How did you hear about us? </td> 
    <td>www.example.com</td> 
    </tr> 
</table> 

我不知道爲什麼會出現這種情況。任何建議將不勝感激。

回答

3

試試這個

$header = "MIME-Version: 1.0\r\n"; 
$header .= "Content-type: text/html; charset: utf8\r\n"; 

也嘗試:PHPMailer

+1

非常感謝它的工作原理 – Toretto

+0

@Tornado你​​很歡迎也檢查[這](http://pear.php.net/manual/en/package.mail.mail-mime.mail-mime。 php) –

+0

但是我需要確認一下,如果我使用'Content-type:text/html;字符集= ISO-8859-1 \ r \ N'。 – Toretto

0

使用

html_entity_decode()

eg. mail($_POST["coach"], $subject, html_entity_decode($message), $headers); 
0

添加以下代碼在接頭連接器P鈕用來

<?php 
$headers .= 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
?>