Newbie, Form "From" address when emailed
Posted: Fri Jul 10, 2009 11:07 pm
I have just created (with a lot of help from tutorials) my first php script to send a form. It seems to be working great, but with one little problem. When it emails me the form, the "from" address it shows in my email is an address I have never seen before. How can I change this to be a familiar address or to the person who is sending the form? Here is the code (although, I'm sure it is veeerrry lacking, as I said, it seems to be getting the info submitted).
<?php
$myemail= "charles@simplecountryloghomes.com";
$subject= "Simple Country Log Homes";
$message= "Web Contact Form";
/* Check all form inputs using check_input function */
$name = check_input($_POST["name"], "Enter your name");
$address = check_input($_POST["address"]);
$city = check_input($_POST['city']);
$state = check_input($_POST['state']);
$zip = check_input($_POST['zip']);
$phone = check_input($_POST['phone'], "Enter your phone number");
$email = check_input($_POST['email'], "Enter your email address");
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $name
Address: $address
City: $city
State: $state
Zip: $zip
Phone: $phone
E-mail: $email
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail("charles@simplecountryloghomes.com,sales@simplecountryloghomes.com,service@disc-n-data.com", $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thankyou.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
<?php
$myemail= "charles@simplecountryloghomes.com";
$subject= "Simple Country Log Homes";
$message= "Web Contact Form";
/* Check all form inputs using check_input function */
$name = check_input($_POST["name"], "Enter your name");
$address = check_input($_POST["address"]);
$city = check_input($_POST['city']);
$state = check_input($_POST['state']);
$zip = check_input($_POST['zip']);
$phone = check_input($_POST['phone'], "Enter your phone number");
$email = check_input($_POST['email'], "Enter your email address");
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $name
Address: $address
City: $city
State: $state
Zip: $zip
Phone: $phone
E-mail: $email
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail("charles@simplecountryloghomes.com,sales@simplecountryloghomes.com,service@disc-n-data.com", $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thankyou.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>