Guillaume Guillaume - 1 month ago 7
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 User@servername.com 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.

<?php
$subject = 'Message from Website'; // Subject of your email
$to = 'me@mydomain.com'; //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';
}
else
{
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>

Answer

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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
Comments