Problem with 'Reply-To' on PHP contact form

Hi guys,

I’ve created a PHP contact form which works perfectly; except, when I receive the email to my inbox and go to reply to it, it doesn’t reply to the email address of the person who submitted the form - in the reply field it appears to have the default server email address.

Here’s my code which I have included in the same PHP page as my form:



<?php

if (array_key_exists('submit', $_POST)) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $formMessage = $_POST['message'];

if (empty($name)) {
$warning['name'] = "Please provide your name";
}

if (empty($email)) {
$warning['email'] = "Please provide your email address";
}

if (empty($formMessage)) {
$warning['message'] = "Please type your message";
}

$headers .= "Reply-To: " . $_POST["email"] . "\\r\
";
$to = "me@myemail.com";
$subject = "Enquiry from website";

$message = $headers;
$message .= "Name: " . $_POST["name"] . "\\r\
";
$message .= "E-mail: " . $_POST["email"] . "\\r\
";

$headers  = "MIME-Version: 1.0\\r\
";
$headers .= "Content-type: text/html; charset=iso-8859-1\\r\
";

$message= "

<table cellspacing='0' cellpadding='8' border='0' width='500'>
<tr>
<td colspan='2'></td>
</tr>
<tr bgcolor='#eeeeee'>
<td width='154' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Name</strong></td>
<td width='314' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$name."</td>
</tr>
<tr bgcolor='#eeeeee'>
<td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>E-mail address:</strong></td>
<td style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$email."</td>
</tr>
<tr bgcolor='#eeeeee'>
<td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'><strong>Message</strong></td>
</tr>
<tr bgcolor='#eeeeee'>
<td colspan='2' style='font-family:Verdana, Arial; font-size:11px; color:#333333;'>".$formMessage."</td>
</tr>
<tr><td colspan='2' style='padding: 0px;'></td></tr>
</table>

";


$url = stripslashes($_POST["url"]);
if (!empty($url)) {
header( 'Location: http://www.go-away-spam-robots.com' );
exit();
}

if (!isset($warning)) {
mail($to, $subject, $message, $headers);
header( 'Location: http://www.mydomain.com/thankyou.php' ) ;
}

}

?>


Can anyone see from my code why this would be happening?

Thank you very much!

SM

Try add "From: " in $headers .
Example:

$headers .= "From: " . $_POST["name"] . "<" . $_POST["email"] . ">\\r\
";

Or:

$headers .= "From: " . $_POST["email"] . "\\r\
";

Hey there,
Thank you for the reply - unfortunately I tried both and neither seemed to work; the email still comes from the default server user and replies to that.
Any other suggestions?
Thanks again :slight_smile:

Hi,
Maybe the cause is from the mail server that is configured to add that address to Reply if the connection is not with SMTP autentification.
Try ask the server administrator.

Thank you - I’ll give them a shout! :slight_smile:

You don’t need the “Reply-to:” header if you have the right “From:” header.

Are you using mail() or imap_mail() to send the emails? I had a similar problem with imap_mail() on my server, it would not recognize my “Return-path:” header. I solved the problem by switching to mail().

You have concatenated your ‘Reply-To’ line without having anything to add it to first:

$headers .= "Reply-To: " . $_POST["email"] . "\\r\
";

It looks as though that gets dropped when you declare $headers as the MIME-Version further down the block:

$headers  = "MIME-Version: 1.0\\r\
";
$headers .= "Content-type: text/html; charset=iso-8859-1\\r\
";

I would concatenate the first line after the next two like so:

$headers  = "MIME-Version: 1.0\\r\
";
$headers .= "Content-type: text/html; charset=iso-8859-1\\r\
";
$headers .= "Reply-To: " . $_POST["email"] . "\\r\
";

I use mail() myself and I’ve had no problems with it.

CASE CLOSED!! :slight_smile:

Thank you very much Enver, that’s absolutely brilliant - that’s solved the mystery!

Also a big thank you to CaptainCSS and MarPlo for your invaluable contributions.

Top shooters, all of you! :slight_smile: