简体   繁体   中英

PHP mail form - email header

I am using a PHP mail form on my site. I receive mail just fine but the headers I am getting are in the form a7849972@srv29.000webhost.com , and the same for reply address. How can I change my code to get the person's name in header? I am using the following code:

<?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";
}
?>

Why are you separating headers into two different variables? You're passing the From and Reply-to headers as additional parameters to the mail() function. Check PHP's documentation .

Try this:

<?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";
}
?>

BTW, you should consider validating the data you're getting in $_POST before concatenating it to your email headers since it can lead to email injection attacks .

Use something like this format in your code:

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

By the way it's better to use utf-8 instead of iso-8859-1 for your charset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM