Guillaume Guillaume - 1 year ago 93
PHP Question

Change the sender address

I am trying to change the from email in the script to the person who sends it so i can reply to the message sent from the form directly. at the moment when a message is sent with the form it appears to come from i would like to change it to the address of the person who fills the form.
I have no knowledge of php, help would be appreciated.

$subject = 'Message from Website'; // Subject of your email
$to = ''; //Recipient's E-mail
$emailTo = $_REQUEST['email'];

$headers = "MIME-Version: 1.1";
$headers .= "Content-type: text/html; charset=iso-8859-1";
$headers .= "From: " . $emailTo . "\r\n"; // Sender's E-mail
$headers .= "Return-Path:". $emailTo;

$message .= 'Name : ' . $_REQUEST['name'] . "\n";
$message .= 'Email : ' . $_REQUEST['email'] . "\n";
$message .= 'Phone : ' . $_REQUEST['phone'] . "\n";
$message .= 'Message : ' . $_REQUEST['message'];

if (@mail($to, $subject, $message, $headers))
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';

Answer Source

I believe your headers are not being read correctly the headers require consistent usage of \r\n to end the header lines. Please try the example from the manual exactly to see if it helps you:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <>, Kelly <>' . "\r\n";
$headers .= 'From: Birthday Reminder <>' . "\r\n";
$headers .= 'Cc:' . "\r\n";
$headers .= 'Bcc:' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download