Newbie, Form "From" address when emailed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
disc-n-data
Forum Newbie
Posts: 2
Joined: Fri Jul 10, 2009 11:00 pm

Newbie, Form "From" address when emailed

Post 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();
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie, Form "From" address when emailed

Post 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).
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Newbie, Form "From" address when emailed

Post 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.
disc-n-data
Forum Newbie
Posts: 2
Joined: Fri Jul 10, 2009 11:00 pm

Re: Newbie, Form "From" address when emailed

Post 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!!!
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Newbie, Form "From" address when emailed

Post 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.
Post Reply