Page 1 of 1

Newbie, Form "From" address when emailed

Posted: Fri Jul 10, 2009 11:07 pm
by disc-n-data
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();
}
?>

Re: Newbie, Form "From" address when emailed

Posted: Fri Jul 10, 2009 11:33 pm
by requinix
The mail() function has a fourth argument: "additional headers".
Take a look at the manual page for how to use it (pay attention to the examples).

Re: Newbie, Form "From" address when emailed

Posted: Sat Jul 11, 2009 1:43 pm
by Eric!
Also filter all your user input fields for email header injection or you might find your server sending out 1,000's of spam messsages from you.

Re: Newbie, Form "From" address when emailed

Posted: Sun Jul 12, 2009 12:25 am
by disc-n-data
Thank you both, I will try the "additional headers". I have no idea how to do the email header injection, can you help with that, or I will look for a tutorial. Thanks again!!!

Re: Newbie, Form "From" address when emailed

Posted: Sun Jul 12, 2009 11:33 am
by Eric!
Look in this thread where I posted the basic idea:

viewtopic.php?f=1&t=102493

It isn't comprehensive, but it will stop the worst problems.