Chander Dhingra
Chander Dhingra

Reputation: 61

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

Upvotes: 1

Views: 16680

Answers (2)

El Barto
El Barto

Reputation: 929

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.

Upvotes: 2

Mohammad Saberi
Mohammad Saberi

Reputation: 13166

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.

Upvotes: 1

Related Questions