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!
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();
?>
seems like you don't receive $_POST['email']; from your contact form (re: "Undefined variable: email ")
check the name of your mail input field...
I'm just starting to learn php, so I suspect this is going to be an easy one. I want to create a form that has one required field (name) and requires at least one of two other fields (email or phone number). This is what I came up with:
<?php
if (isset($_POST["submit"])) {
if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {
$error = "Your name and a contact method are required fields";}
else {
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$contactmethod = $_POST["contactmethod"];
$subject = $_POST["subject"];
$location = $_POST["location"];
$message = $_POST["message"];
$subjectline = "You have a message from $name!";
$header = "From: $email";
$body = <<<EMAIL
Name: $name,
Number: $number,
Email: $email,
Subject: $subject,
Location: $location,
Contact method: $contactmethod.
$message
EMAIL;
mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
}
?>
I have no clue what's wrong.
Thanks
Your problem: You used { and } in a if-statement.
Next time you can use a PHP code checker like http://phpcodechecker.com/
Correct code:
if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {
$error = "Your name and a contact method are required fields";}
else {
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$contactmethod = $_POST["contactmethod"];
$subject = $_POST["subject"];
$location = $_POST["location"];
$message = $_POST["message"];
$subjectline = "You have a message from $name!";
$header = "From: $email";
$body = <<<EMAIL
Name: $name,
Number: $number,
Email: $email,
Subject: $subject,
Location: $location,
Contact method: $contactmethod.
$message
EMAIL;
mail("MYEMAILADDRESS", $subjectline, $body, $header);
}
}
?>
You can't use braces interchangeably with parentheses.
if (empty($_POST["name"]) or {empty($_POST["email"]) and empty($_POST["number"])}) {
it needs to use parentheses
if (empty($_POST["name"]) or (empty($_POST["email"]) and empty($_POST["number"]))) {
I have a contact form that has certain conditions. If the user clicks the radio button saying they are a Consumer, a new range of questions appears. If they click that they have an image to upload, the image uploader appears.
As it is now, the email that is sent to us and to the user shows all of the fields, even if blank. It has the potential to get confusing if the person didn't click that they were a consumer, only to see 3 extra questions in their email confirmation that they didn't even see on the form.
I have it set up so there is a $success that should send the message to us and the user without all of the consumer questions, $successConsumer that should send the message to us and the user with consumer questions, but without the image, and $successOther that should send the message to us and the user with consumer questions AND the image.
I have tried including the variable in with the if statements, i.e
if ($_FILES["uploaded_file"]["size"] == 0) {
$successOther = mail($email_address, $subject, $messageOther, $headers);
$successOther = mail($email_address, $subject, $messageOther2, $headers);
header("Location: thanks.php");
}
with no luck, as well as using eval(), return(), and exit() in similar fashions. It either doesn't work at all, or it will send me all 6 messages, not just 2. I'm sure there is a way to do this, but I'm just not grasping how it's done. I've tried multiple Google searches, but I'm coming up with nothing. Any help or direction is really appreciated!
<?php
//Collect contact form data
//Check Special Field
//Email ASC & Webmaster
//Email Sender
//Redirect to thank you page
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/functions.php');
/******** CONTACT DATA **********/
$name = stripslashes($_POST['name']);
$company = stripslashes($_POST['company']);
$address = stripslashes($_POST['address']);
$city = stripslashes($_POST['city']);
$state = stripslashes($_POST['state']);
$zipcode = stripslashes($_POST['zipcode']);
$country = stripslashes($_POST['country']);
$website = $_POST['website'];
$phone = stripslashes($_POST['phone']);
$fax = stripslashes($_POST['fax']);
$email = stripslashes($_POST['contact']);
$Referred = stripslashes($_POST['referred']);
$CustomerType = stripslashes($_POST['CustomerType']);
$Comments = stripslashes($_POST['comments']);
$ConsumerHelp = stripslashes($_POST['ConsumerHelp']);
$UPC = stripslashes($_POST['UPC']);
$uploaded_file = ($_FILES['uploaded_file']);
if ($_FILES["uploaded_file"]["size"] == 0) {
header("Location: thanks.php");
}
else {
// there is a file
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["uploaded_file"]["name"]);
$extension = end($temp);
$userImage = $_FILES["uploaded_file"]["name"];
$location = str_replace(' ', '_', $location);
if ((($_FILES["uploaded_file"]["type"] == "image/gif")
|| ($_FILES["uploaded_file"]["type"] == "image/jpeg")
|| ($_FILES["uploaded_file"]["type"] == "image/jpg"))
&& ($_FILES["uploaded_file"]["size"] < 2300000 )
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "<br>";
} else {
header("Location: thanks.php");
if (file_exists("uploads/" . $_FILES["uploaded_file"]["name"])) {
echo $_FILES["uploaded_file"]["name"] . " already exists. ";
} else {
$location = "uploads/" . $temp[0].".".time().".".$extension;
$location = str_replace(' ', '_', $location);
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $location);
echo "Stored in: " . $location;
}
}
} else {
header("Location: sorry.php");
exit ();
}
}
/******** NOT CONSUMER EMAIL ASC & WEBMASTER **********/
$message = "
-----------------------------------------------------------------------------
Information Inquiry
-----------------------------------------------------------------------------
$name has visited the web site and would like some information.
The details they entered on the website are:
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC
Image: http://www.example.com/test/$location
(If the consumer sent the form without attaching an image, the link above will not have the file name at the end - just ignore it.)
Kind Regards,
";
$email_address = "example#example.com";
$subject = "Information Inquiry";
$headers = "From: $name <$email>";
$message = str_replace("\r",'',$message); //fixes postfix php bug that double spaces messages
/******** NOT CONSUMER EMAIL SENDER **********/
$message2 = "
-----------------------------------------------------------------------------
Re: Information Inquiry
-----------------------------------------------------------------------------
Thank you $name for visiting the web site. We will be using the details you entered to contact you.
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC
Image: $userImage
Kind Regards,
";
$email_address2 = "$email";
$subject2 = "Re: Information Inquiry";
$headers2 = "From: Example <example#example.com>";
$message2 = str_replace("\r",'',$message2); //fixes postfix php bug that double spaces messages
/******** CONSUMER - WITH IMAGE - EMAIL ASC & WEBMASTER **********/
$messageConsumer = "
-----------------------------------------------------------------------------
Information Inquiry
-----------------------------------------------------------------------------
$name has visited the web site and would like some information.
The details they entered on the website are:
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC
Image: http://www.example.com/test/$location
Kind Regards,
";
$email_address = "example#example.com";
$subject = "Information Inquiry";
$headers = "From: $name <$email>";
$messageConsumer = str_replace("\r",'',$messageConsumer); //fixes postfix php bug that double spaces messages
/******** CONSUMER - WITH IMAGE - EMAIL SENDER **********/
$messageConsumer2 = "
-----------------------------------------------------------------------------
Re: Information Inquiry
-----------------------------------------------------------------------------
Thank you $name for visiting the web site. We will be using the details you entered to contact you.
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC
Image: $userImage
Kind Regards,
";
$email_address2 = "$email";
$subject2 = "Re: Information Inquiry";
$headers2 = "From: <example#example.com>";
$messageConsumer2 = str_replace("\r",'',$messageConsumer2); //fixes postfix php bug that double spaces messages
/******** CONSUMER - NO IMAGE - EMAIL ASC & WEBMASTER **********/
$messageOther = "
-----------------------------------------------------------------------------
Information Inquiry
-----------------------------------------------------------------------------
$name has visited the web site and would like some information.
The details they entered on the website are:
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC
Kind Regards,
";
$email_address = "example#example.com";
$subject = "Information Inquiry";
$headers = "From: $name <$email>";
$messageOther = str_replace("\r",'',$messageOther); //fixes postfix php bug that double spaces messages
/******** CONSUMER - NO IMAGE - EMAIL SENDER **********/
$messageOther2 = "
-----------------------------------------------------------------------------
Re: Information Inquiry
-----------------------------------------------------------------------------
Thank you $name for visiting the web site. We will be using the details you entered to contact you.
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC
Kind Regards,
";
$email_address2 = "$email";
$subject2 = "Re: Information Inquiry";
$headers2 = "From: <example#example.com>";
$messageOther2 = str_replace("\r",'',$messageOther2); //fixes postfix php bug that double spaces messages
$success = mail($email_address, $subject, $message, $headers);
$successConsumer = mail($email_address, $subject, $messageConsumer, $headers);
$successOther = mail($email_address, $subject, $messageOther, $headers);
//conditionally send message2, no need to check success on this one
if (strpos($email,'#aol.com') == false) {
mail($email_address2, $subject2, $message2, $headers2);
$successConsumer = mail($email_address, $subject, $messageConsumer2, $headers);
$successOther = mail($email_address, $subject, $messageOther2, $headers);
}
if (!$success) {
// What happens when the form does not validate
header("Location: sorry.php");
die ();
} else {
// Your code here to handle a successful verification
// header("Location: thanks.php");
$success;
}
?>
Edit
I tried replacing the if statement with the code below... I don't get any mail now. Any thoughts?
if ( isset($_POST['CustomerType']) )
{
if ( $_POST['CustomerType'] === 'Consumer')
{
$success = mail ($headersCoastal, $subjectCoastal, $messageConsumer, $email_address);
$success = mail ($headersUser, $subjectUser, $messageConsumer2, $email);
// header("Location: thanks.php");
}
else
{
$success = mail ($headersCoastal, $subjectCoastal, $messageOther, $email_address);
$success = mail ($headersUser, $subjectUser, $messageOther2, $email);
// header("Location: thanks.php");
}
};
Edit
<?php
// Vars
$name = filter_input( INPUT_POST, 'name' );
$email = filter_input( INPUT_POST, 'email' );
//$customer = filter_input( INPUT_POST, 'customer' );
$company = filter_input( INPUT_POST, 'company' );
$address = filter_input( INPUT_POST, 'address' );
$city = filter_input( INPUT_POST, 'city' );
$state = filter_input( INPUT_POST, 'state' );
$zipcode = filter_input( INPUT_POST, 'zipcode' );
$country = filter_input( INPUT_POST, 'country' );
$website = filter_input( INPUT_POST, 'website' );
$phone = filter_input( INPUT_POST, 'phone' );
$fax = filter_input( INPUT_POST, 'fax' );
$email = filter_input( INPUT_POST, 'email' );
$Referred = filter_input( INPUT_POST, 'Referred' );
$CustomerType = filter_input( INPUT_POST, 'CustomerType' );
$Comments = filter_input( INPUT_POST, 'comments' );
$ConsumerHelp = filter_input( INPUT_POST, 'ConsumerHelp' );
$UPC = filter_input( INPUT_POST, 'UPC' );
$uploaded_file = ($_FILES['uploaded_file']);
$to_asc = 'example#gmail.com';
$to_webmaster = 'example#example.com';
$to_user = $email;
$subject = 'Information Inquiry';
$from = 'ACME WIDGET COMPANY';
$headers = "From: " . $to_asc . "\r\n";
$headers .= "Reply To: " . $to_asc . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<p>Thank you ' . $name . ' for visiting the Coastal Pet Products web site. We will be using the details you entered to contact you.</p>';
// Check for customer status.
if ( isset( $CustomerType ) )
{
if ( $CustomerType == 'Consumer')
{
$message .= '<p>Name: $name
</br>Company: $company
</br>Address: $address
</br>City: $city
</br>State: $state
</br>Zip Code: $zipcode
</br>Country: $country
</br>Website: $website
</br>Phone: $phone
</br>Fax: $fax
</br>Email: $email
</br>Referred to web site: $Referred
</br>CustomerType: $CustomerType
</br>Comments: $Comments
</br>I need help with: $ConsumerHelp
</br>UPC code or Item #: $UPC';
header ("Location: thanks.php");
}
else
{
$message .= '<p>Name: $name
</br>Company: $company
</br>Address: $address
</br>City: $city
</br>State: $state
</br>Zip Code: $zipcode
</br>Country: $country
</br>Website: $website
</br>Phone: $phone
</br>Fax: $fax
</br>Email: $email
</br>Referred to web site: $Referred
</br>CustomerType: $CustomerType
</br>Comments: $Comments';
header ("Location: thanks.php");
}
}
else // Input was not selected at all.
{
header("Location: sorry.php");
die ();;
}
// You can look/set other conditions as you go to further assemble a specific message to the user/admins.
// Now back to a chunk that all users see.
//$message .= '<p>Oh well, it\'s all good in the hood!</p>';
//echo $message;
// Mail the message.
if (mail($to_user, $subject, $message, $headers))
{
// Mail to user was sent. Now send email to admin(s).
mail($to_asc, $subject, $message, $headers);
}
Final Edit
I was able to get everything working with the code below. The only changes I need to make now are minor, such as setting it so the email that goes to the user says that it is from us, not them. Not a huge deal though! :)
<?php
// Vars
$name = stripslashes($_POST['name']);
$company = stripslashes($_POST['company']);
$address = stripslashes($_POST['address']);
$city = stripslashes($_POST['city']);
$state = stripslashes($_POST['state']);
$zipcode = stripslashes($_POST['zipcode']);
$country = stripslashes($_POST['country']);
$website = $_POST['website'];
$phone = stripslashes($_POST['phone']);
$fax = stripslashes($_POST['fax']);
$email = stripslashes($_POST['contact']);
$Referred = stripslashes($_POST['referred']);
$CustomerType = stripslashes($_POST['CustomerType']);
$Comments = stripslashes($_POST['comments']);
$ConsumerHelp = stripslashes($_POST['ConsumerHelp']);
$UPC = stripslashes($_POST['UPC']);
$uploaded_file = ($_FILES['uploaded_file']);
$to_asc = 'example#example.com';
$to_webmaster = 'example#example.com';
$to_user = $email;
$subject = 'Information Inquiry';
$from = 'My Company';
$headers = "From: $name <$email>";
//$headers .= "Reply To: " . $to_asc . "\r\n";
$message .= "Thank you, " . $name . ", for visiting the web site. We will be using the details you entered to contact you.";
if ($_FILES["uploaded_file"]["size"] == 0) {
header("Location: thanks.php");
}
else {
// there is a file
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["uploaded_file"]["name"]);
$extension = end($temp);
$userImage = $_FILES["uploaded_file"]["name"];
$location = str_replace(' ', '_', $location);
if ((($_FILES["uploaded_file"]["type"] == "image/gif")
|| ($_FILES["uploaded_file"]["type"] == "image/jpeg")
|| ($_FILES["uploaded_file"]["type"] == "image/jpg"))
&& ($_FILES["uploaded_file"]["size"] < 2300000 )
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "<br>";
} else {
header("Location: thanks.php");
if (file_exists("uploads/" . $_FILES["uploaded_file"]["name"])) {
echo $_FILES["uploaded_file"]["name"] . " already exists. ";
} else {
$location = "uploads/" . $temp[0].".".time().".".$extension;
$location = str_replace(' ', '_', $location);
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $location);
echo "Stored in: " . $location;
}
}
} else {
header("Location: sorry.php");
exit ();
}
}
// Check for customer status.
if ( isset( $CustomerType ) )
{
if ( $CustomerType == 'Consumer' && ($_FILES["uploaded_file"]["size"] == 0) )
{
$message .= "
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC";
header ("Location: thanks.php");
}
if ( $CustomerType == 'Consumer' && ($_FILES["uploaded_file"]["size"] >= 1) )
{
$message .= "
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments
I need help with: $ConsumerHelp
UPC code or Item #: $UPC
Image: http://www.example.com/test/$location";
header ("Location: thanks.php");
}
if ( $CustomerType !== 'Consumer')
{
$message .= "
Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email
Referred to web site: $Referred
CustomerType: $CustomerType
Comments: $Comments";
header ("Location: thanks.php");
}
}
else // Input was not selected at all.
{
header("Location: sorry.php");
die ();;
}
// Mail the message.
if (mail($to_user, $subject, $message, $headers))
{
// Mail to user was sent. Now send email to admin(s).
mail($to_asc, $subject, $message, $headers);
}
Give this a try. I'm not able to test the actual mail() function at work. However, when I echo out $message i'm getting what I would expect in an email.
HTML:
<!doctype html>
<head>
<title>Email Form</title>
</head>
<body>
<form id="myForm" action="submit.php" method="post">
<fieldset>
<legend>Customer Form</legend>
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="Robert Paulson" />
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="robert.paulson#email.com" />
</fieldset>
<fieldset>
<p>Are you a customer?</p>
<input type="radio" name="customer" id="customerY" value="yes" /><label for="customerY">Yes</label>
<input type="radio" name="customer" id="customerN" value="no" /><label for="customerN">No</label>
</fieldset>
<fieldset>
<input type="submit" value="Submit" />
</fieldset>
</form>
</body>
</html>
PHP: (submit.php)
<?php
// Vars
$name = filter_input( INPUT_POST, 'name' );
$email = filter_input( INPUT_POST, 'email' );
$customer = filter_input( INPUT_POST, 'customer' );
$to_asc = 'asc#email.com';
$to_webmaster = 'webmaster#email.com';
$to_user = $email;
$to_isCustomer = 'is.customer#email.com';
$to_isNotCustomer = 'not.customer#email.com';
$subject = 'Information Inquiry';
$from = 'ACME WIDGET COMPANY';
$headers = "From: " . $to_asc . "\r\n";
$headers .= "Reply To: " . $to_asc . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<p>Hi ' . $name . ', thanks for whatev.</p>';
// Check for customer status.
if ( isset( $customer ) )
{
if ( $customer == 'yes')
{
$message .= '<p>Because you are a valued coustomer, we\'ll respond ASAP!</p>';
}
else
{
$message .= '<p>Because you are not a coustomer, we\'ll respond sometime - you know, before the house burns down.</p>';
}
}
else // Input was not selected at all.
{
$message .= '<p>Because you didn\'t check an option - I don\'t know what to call you.</p>';
}
// You can look/set other conditions as you go to further assemble a specific message to the user/admins.
// Now back to a chunk that all users see.
$message .= '<p>Oh well, it\'s all good in the hood!</p>';
echo $message;
// Mail the message.
if (mail($to_user, $subject, $message, $headers))
{
// Mail to user was sent. Now send email to admin(s).
mail($to_asc, $subject, $message, $headers);
}
For example, this is what renders if the user does not select a customer option at all:
Hi bob, thanks for whatev.
Because you didn't check an option - I don't know what to call you.
Oh well, it's all good in the hood!
This might take some re-working, but I would try to cut down the amount of re-doing things.
By that I mean have 1 mail() call.
I'm greatly over-simplifying here, but this should give you an idea of what I'm talking about. In essence, you would piece together the message rather than repeat yourself over and over with different mail calls/settings.
NOTE: be sure to sanitize all data!
HTML:
<form action="bla.php" method="post">
<input type="text" name="name" />
<input type="email" name="email" />
<radio name="customer" value="yes" />
<radio name="customer" value="no" />
<input type="submit" value="submit" />
</form>
PHP:
<?php
$to = '';
$submit = '';
$message = '';
$headers = 'SOME_HEADER_VALUES';
$name = $_POST['name'];
$email = $_POST['email'];
$admin_eamil = 'admin#email.com';
$message .= 'Hello, thanks for whatev.';
if ( isset[$_POST['customer']) )
{
if ( $_POST['customer'] === 'yes')
{
$subject = 'Re: Hi Customer';
$message .= 'Glad you\'re a customer!';
}
else
{
$subject = 'Re: Meh, whatever';
$message .= 'We are ignoring you since you are not a customer';
}
}
else
{
...
}
// whatever else conditions you need could go here
...
$message .= 'Glad we got that all cleared up.';
if (mail($to, $subject, $message, $headers))
{
mail($admin_eamil, $subject, $message, $headers);
}
I am facing problems in sending e-mail through my contact form
I can not determine where is the problem I think that I have done everything right but I am here to take the advice and I hope you will help me.
<?php
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['body']) && isset($_POST['subject'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['body'];
$subject = $_POST['subject'];
$to = 'samir1986#gmail.com';
$header = 'From: '.$name.'<'.$email.'>';
if(!empty($name) && !empty($email) && !empty($body)){
mail($to, $subject, $message,$header);
echo 'An e-mail have been sent successfully';
}
else{
echo 'Please try sending e-mail again';
}
}
?>
Please make sure you've checked the value of each of your fields (for security reasons) then do :
<?php
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['body']) && !empty($_POST['subject'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['body'];
$subject = $_POST['subject'];
$to = 'samir1986#gmail.com';
$header = 'From: '.$name.' <'.$email.'>'."\r\n";
if (mail($to, $subject, $message, $header))
{
echo 'An e-mail have been sent successfully';
}
else
{
echo 'Please try sending e-mail again';
}
}
?>
iv added the header field in the form code but when i receive the email, the "from" field in the email shows a garbled code and not the email address of the person who sent the message. This is my message processing code:
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty($_SESSION['chapcha_code'] ) ) {
$youremail = 'I removed my address for privacy on stack overflow';
$fromsubject = 'A message from your website';
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['mail'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From: $nome <$mail>\r\n";
$to = $youremail;
$mailsubject = 'Message received from'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is '.$fname.' '.$lname.'
Address: '.$address.'
'.$city.', '.$zip.', '.$country.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your feedback. I will contact you shortly if needed.<br/>Go to <a href='/index.php'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
You declared your ^From: header` here:
$headers = "From: $nome <$mail>\r\n";
But it isn't used when you call mail()
mail($to, $subject, $body);
You need to pass $headers as fourth parameter. Else the headers won't take effect
mail($to, $subject, $body, $headers);
You haven't declared $nome anywhere in your code nor $mail in:
$headers = "From: $nome <$mail>\r\n";
And besides that, as this comment mentions, you didn't pass the header into the mail function. See the fourth parameter of the mail function.