2012-03-11 44 views
0

我在我的網站上使用PHP郵件表單。我收到的郵件很好,但是我收到的郵件的格式爲[email protected],回覆地址也一樣。我如何更改我的代碼以在頭中獲取該人的姓名?我使用下面的代碼:PHP郵件表單 - 電子郵件標題

<?php 
if(isset($_POST['submit'])) { 
    $to = '[email protected]' ;  //put your email address on which you want to  receive the information 
    $subject = 'Message - Contact Form Coast Med Spa'; //set the subject of email. 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $mailheader = "From: ".$_POST["FirstName"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $message = "<table> 
      <tr><td>Title</td><td>".$_POST['Title']."</td></tr> 
      <tr><td>First Name</td><td>".$_POST['FirstName']."</td></tr> 
      <tr><td>Last Name</td><td>".$_POST['LastName']."</td></tr> 
      <tr><td>E-Mail</td><td>".$_POST['Email']."</td></tr> 
      <tr><td>Phone Number</td><td>".$_POST['HomePhone']."</td></tr> 
      <tr><td>Comments</td><td>".$_POST['CAT_Custom_869']."</td></tr> 
      <tr><td>Contact Method</td><td>".$_POST['CAT_Custom_868']."</td></tr> 
      <tr><td>Subscribe to: eNewsletter</td>  <td>".$_POST['CampaignList_41798']."</td></tr> 
      </table>" ; 
    mail($to, $subject, $message, $headers, $mailheader); 
    header('Location: http://coastlasercenter.com/html/message-contact.html'); 
echo "Your message has been received"; 
} 
?> 
+0

你最好使用像'PHPmailer'這樣的郵件類。內建的mail()函數不適合當今的電子郵件標準,很難修改這些東西。 – Corubba 2012-03-11 12:59:41

回答

2

你爲什麼要分離標題變爲兩個不同的變量? 您將FromReply-to標題作爲mail()函數的附加參數傳遞。檢查PHP的documentation

試試這個:

<?php 
if(isset($_POST['submit'])) { 
    $to = '[email protected]' ;  //put your email address on which you want to  receive the information 
    $subject = 'Message - Contact Form Coast Med Spa'; //set the subject of email. 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $headers .= "From: ".$_POST['FirstName']." ".$_POST['LastName']." <".$_POST['Email'].">\r\n"; 
    $headers .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $message = "<table> 
      <tr><td>Title</td><td>".$_POST['Title']."</td></tr> 
      <tr><td>First Name</td><td>".$_POST['FirstName']."</td></tr> 
      <tr><td>Last Name</td><td>".$_POST['LastName']."</td></tr> 
      <tr><td>E-Mail</td><td>".$_POST['Email']."</td></tr> 
      <tr><td>Phone Number</td><td>".$_POST['HomePhone']."</td></tr> 
      <tr><td>Comments</td><td>".$_POST['CAT_Custom_869']."</td></tr> 
      <tr><td>Contact Method</td><td>".$_POST['CAT_Custom_868']."</td></tr> 
      <tr><td>Subscribe to: eNewsletter</td>  <td>".$_POST['CampaignList_41798']."</td></tr> 
      </table>" ; 
    mail($to, $subject, $message, $headers); 
    header('Location: http://coastlasercenter.com/html/message-contact.html'); 
echo "Your message has been received"; 
} 
?> 

BTW,你應該考慮它串聯到您的電子郵件標題,因爲它可以導致email injection attacks前驗證你要在$ _ POST數據。

+0

另一件事,看看你的代碼的其餘部分:如果你將客戶端重定向到另一個頁面,他將無法看到發送Location標頭後回顯的內容。你應該在message-contact.html上顯示你的信息 – 2012-03-11 13:29:40

+0

yes echo我忘記刪除它只用於測試......驗證我聯繫我們-html文件 – 2012-03-11 15:29:31

+0

我在腳本中建議的更改是否可以幫助你? – 2012-03-11 15:38:29

1

使用這樣的格式代碼:

$headers .= "From: ".$userName." <".$userEmailAddress.">"; 

通過它更好地利用ISO-8859-1UTF-8,而不是你的字符集的方式。

+0

我必須輸入什麼值。$ userName和$ userEmailAddress。 – 2012-03-12 02:54:06

+0

$ userName必須是'$ _POST [「FirstName」]'和$ userEmailAddress必須是'$ _POST [「email」]' – 2012-03-12 07:13:32

+0

HURRAY!它對馬德里Sabrei和AndrésGattinoni都很感謝。即使我加了最後一個名字也是'$ headers。=「From:」。$ _ POST ['FirstName']。「」。$ _ POST ['LastName']。「<」。$ _ POST ['Email']。「> 「;' – 2012-03-12 07:26:35