0

When a user inputs their email on a form on my site, I get an email with their info. I'd like to be able to reply to that email and have their email autofill in "To:" but I'm having trouble. I found this question and tried the solution: reply-to address in php contact form but it's not working for me, and I'm not sure why.

Here is my PHP:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$to = 'amanda@myemail.com';
$headers = "BCC: clients@myemail.com\r\n";  
$headers .= 'Reply-To: ' . $email . "\r\n";

$subject = '*** Quote Request';

$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];


$message = <<<EMAIL

Quote submission from: $name

Name: $name
Phone Number: $phone
Date: $date
Time: $time
Pickup Location: $pickup
Drop Off Location: $dropoff
Total Passengers: $passengers
Service needed: $service
Email: $email

EMAIL;


if($_POST) {
    mail($to, $subject, $message, $headers);
}
header('Location: thankyou.html');
exit();

?>

And this is the error message I'm getting, summed up:

Undefined variable: email in /contact-form-handler.php on line 9 Warning: Cannot modify header information - headers already sent by (output started at /contact-form-handler.php:9) in /contact-form-handler.php on line 44

The problem is the variable $email because if I put a Reply-To and specify an email, it works. I thought maybe it was because the variable is defined after I call it in the header, but adding it to the bottom didn't work. I'm a rookie with PHP so I'm not sure why this variable isn't working.

I also tried:

$headers = "BCC: clients@myemail.com\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Any help would be appreciated!

Community
  • 1
  • 1
amandathewebdev
  • 259
  • 5
  • 22
  • 1
    Adding it in the bottom where? Generally you can't use a variable before you assign a value to it. – Jakub Kania Mar 31 '16 at 16:39
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – chris85 Mar 31 '16 at 16:42
  • 1
    It's been said many times before - do not use the internal PHP `mail` function because its only a wrapper for MTAs like sendmail which imposes many restrictions and regularly causes problems. Instead use SMTP through proven solutions like `PHPMailer` (https://github.com/PHPMailer/PHPMailer) or `SwiftMailer` (https://github.com/swiftmailer/swiftmailer). –  Mar 31 '16 at 16:45
  • Are you intentionally swapping quote types on a single string? `$headers .= 'Reply-To: ' . $email . "\r\n";` is using both single and double quotes. This looks horrifically bad practise, to me. – Martin Mar 31 '16 at 16:46

2 Answers2

1

You use a variable before you define it!

Move this block to the top of your script:

$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];

Like that:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];

$to = 'amanda@myemail.com';
$headers = "BCC: clients@myemail.com\r\n";  
$headers .= 'Reply-To: ' . $email . "\r\n";

$subject = '*** Quote Request';


$message = <<<EMAIL

Quote submission from: $name

Name: $name
Phone Number: $phone
Date: $date
Time: $time
Pickup Location: $pickup
Drop Off Location: $dropoff
Total Passengers: $passengers
Service needed: $service
Email: $email

EMAIL;


if($_POST) {
    mail($to, $subject, $message, $headers);
}
header('Location: thankyou.html');
exit();

?>
Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
  • Thank you Kevinrob! I can't believe it was that simple, I feel pretty silly. But I really appreciate you figuring it out, that worked for me. – amandathewebdev Mar 31 '16 at 16:58
0

seems like you don't receive $_POST['email']; from your contact form (re: "Undefined variable: email ")

check the name of your mail input field...

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • That would be an undefined **index** notice, not undefined **variable**. This line `$headers .= 'Reply-To: ' . $email . "\r\n";` throws it because it was not defined previously. – chris85 Mar 31 '16 at 17:07