我在我的网站上使用PHP邮件表单。我收到的邮件很好,但我收到的标题是a7849972@srv29.000webhost.com
形式,回复地址相同。如何更改我的代码以在标题中获取此人的姓名?我使用以下代码:
<?php
if(isset($_POST['submit'])) {
$to = 'chander_info@yahoo.com' ; //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 :(得分:2)
为什么要将标题分成两个不同的变量?
您将From
和Reply-to
标题作为附加参数传递给mail()
函数。检查PHP的documentation。
试试这个:
<?php
if(isset($_POST['submit'])) {
$to = 'chander_info@yahoo.com' ; //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";
}
?>
顺便说一下,你应该考虑验证你在$ _POST中获得的数据,然后再将它连接到你的电子邮件标题,因为它可以导致email injection attacks。
答案 1 :(得分:1)
在代码中使用类似格式的内容:
$headers .= "From: ".$userName." <".$userEmailAddress.">";
顺便说一句,最好使用 utf-8 代替 iso-8859-1 作为你的字符集。