How to change from email address in php contact form

I have build a PHP contact form with validation which works fine .
But receiving problem. when submit i receive email but it is showing wrong email address in from area.
I want to show my actual address.
Here is live link http://www.luxpress.co.uk/checking/contact_form.php
when you submit enter your own email address you will receive confirmation email also.
check what i receive in from area.
from: me@example.com
to: you@example.com
date: Wed, Aug 31, 2016 at 12:38 AM
I want to show me@example.com

Here is my PHP validation form code

<?php
// Initialize variables to null.
$name ="";		//Sender Name
$email =""; 	//Sender's email ID
$purpose ="";	//Subject of mail
$message ="";	//Sender's Message 

$nameError ="";			
$emailError ="";
$purposeError ="";
$messageError ="";
$successMessage ="";

// Additional headers

$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";

//On submitting form below function will execute

if(isset($_POST['submit']))
  {
  // checking null values in message
    if (empty($_POST["name"])){
        $nameError = "Name is required";
      } 
   else {
       $name = test_input($_POST["name"]);
       // check name only contains letters and whitespace
       if (!preg_match("/^[a-zA-Z ]*$/",$name)){
            $nameError = "Only letters and white space allowed"; 
         }
     }
 // checking null values in message  
   if (empty($_POST["email"])) {
       $emailError = "Email is required";
      } 
   else {
      $email = test_input($_POST["email"]);
      }
 // checking null values in message    
   if (empty($_POST["purpose"])) {
      $purposeError = "Purpose is required";
     }
   else { 
	  $purpose = test_input($_POST["purpose"]);  
	 } 
// checking null values in message
   if (empty($_POST["message"])) {
      $messageError = "Message is required";
     } 
   else { 
	  $message = test_input($_POST["message"]);  
	 } 
  // checking null values in all fields  
if( !($name=='') && !($email=='') && !($purpose=='') &&!($message=='') )

  {// checking valid email
    if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
      
		$header= $name."<". $email .">";
                $headers = "CONTACT FORM";
     /* Let's prepare the message for the e-mail */
		$msg = "Hello! $name

 Thank you...! For Contacting Us.

 Name: $name
 E-mail: $email
 Purpose: $purpose
 Message: $message 
  
 This is a Contact Confirmation mail.
 We Will contact You as soon as possible.";

$msg1 = " $name Contacted Us.

 Here are some information about $name.

 Name: $name
 E-mail: $email
 Purpose: $purpose
 Message: $message ";

/* Send the message using mail() function */
  if(mail($email, $headers, $msg ) && mail("you@example.com", $header, $msg1 ))
    {
	$successMessage = "Thank you and that Red Dragon Digital Services received their enquiry and someone will get back to them.";
    }
  }
else { $emailError = "Invalid Email"; }

 }
}
// function for filtering input values
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>

Plz help me to solve this issue

Problem is that here you append a from-address onto $headers:

// Additional headers

$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";

but later on, you blank all that out and start again:

                $headers = "CONTACT FORM";
     /* Let's prepare the message for the e-mail */

I don’t see why you’d append something in the very start of the code as $headers has no value at that point, which suggests that’s later code that has been added, but in the wrong place. But other than in that location, I can’t see anywhere you set a from-address.

so what you suggest how it should be look like

Well, you’ve either got to specify the from-address in the message as you would the subject, or put it in the additional headers. I haven’t done much with the mail() function - does this help: http://php.net/manual/en/function.mail.php

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.