1

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 [email protected], 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 = '[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";
}
?>
1
  • You better use a mailer class like PHPmailer. The build-in mail() function doesn't fit today's email standards and is hard to modify for such things.
    – Corubba
    Mar 11, 2012 at 12:59

2 Answers 2

2

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 = '[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, 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.

8
  • Another thing, looking at the rest of your code: if you're redirecting the client to another page, he won't be able to see what you echo after sending the Location headers. You should display your message on message-contact.html
    – El Barto
    Mar 11, 2012 at 13:29
  • yes echo i have forget to remove it was there for testing only...the validation i have in contact-us.html file Mar 11, 2012 at 15:29
  • Did the changes I suggested on your script help you?
    – El Barto
    Mar 11, 2012 at 15:38
  • Look at the headers of the email you're receiving, there might be something there that helps you to understand what's happening. There's also the chance that 000webhost is rewriting your email headers. If this is the case, there's not much you can do from your script. You should contact the server administrators for that.
    – El Barto
    Mar 11, 2012 at 18:28
  • In fact 000webhost i am using for testing only the main site coastmedspa.com and on their webmail also I am having the same issue, though there sender is [email protected] in the header Mar 11, 2012 at 18:34
1

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.

3
  • What values I have to put in .$userName and .$userEmailAddress. Mar 12, 2012 at 2:54
  • $userName must be $_POST["FirstName"] and $userEmailAddress must be $_POST["email"] Mar 12, 2012 at 7:13
  • HURRAY! it worked thanks a lot to both Md. Saberi and Andrés Gattinoni . Even I added last name it too $headers .= "From: ".$_POST['FirstName']." ".$_POST['LastName']." <".$_POST['Email'].">"; Mar 12, 2012 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.