Hi there Foxhole,
Right then, this is extremely simplified code, there is no error checking or blank entry checking here this is just used to illustrate how to do a simple mail script, and as it is literally typed "on-the-fly", there is more than likely some errors in there. I am just trying to show you some of the building blocks of doing a simple mail script.
If you leave the action="" attribute blank, the form by default will post back to itself, hence why I have done the php code in the same file.
There are other things as you could add to this to make it secure, again, this is purely as an illustration.
I have used the strip_tags() function on the inputs to show that you can try to stop any bad code being submitted that may harm your site, use mysql_real_escape_string() to do the same to any data that is being used in conjunction with any sql queries.
Hope this helps you a bit, and good luck finding a decent book.
A good website to check out is: [
w3schools.com ] This is excellent if you want to learn coding to standards set out by the W3C people, I learnt a lot here.
And of course: [
php.net ] also the mail function directly: [
uk.php.net ]
The action attribute is used to tell the form where it should look to send the data that is being submitted by the form, so in effect this is where you are sending the data, this can either be the same file ((action="")default) or to a specific file ie action="mymailfile.php" when you specify a file, make sure that the file path is used, or if its in the same directory as the form file, use this: action="/mymailfile.php"
Anyway, have fun!
<?php
if(isset($_POST['submit']) && ($_POST['submit'] == "send comments")){
//assign vars
$formname = trim(strip_tags($_POST['name']));
$formemail = trim(strip_tags($_POST['email']));
$formsubject = trim(strip_tags($_POST['subject']));
$formcomments = trim(strip_tags($_POST['comments']));
$mailheaders = "MIME-version: 1.0\r\n";
$mailheaders .= "content-type: text/plain; charset=UTF-8\r\n";
$mailheaders .= "your friendly webmaster! <someone@somesite.com>\r\n";
mail($formemail, $formsubject, $formcomments, $mailheaders);
?><!--break out of php here to allow easier html editing-->
<html>
<meta refresh>can be used to redirect the page back to homepage etc
<body>
<div>Thanks for the mail, see you again soon!</div>
</body>
</html>
<?php
//break back in to close script
exit;//this terminates the script so that you won't get the form echoed twice :)
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Email Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<base href="" />
<meta name="description" content="" />
<meta name="keywords" content="" />
</head>
<body>
<form action="" method="post">
<p>
Your name:<input type="text" name="name" />
</p>
<p>
Your email:<input type="text" name="email" />
</p>
<p>
The subject:<input type="text" name="Subject" />
</p>
<p>
Your Comments:
</p>
<p>
<textarea name="comments" cols="50" rows="25"></textarea>
</p>
<p>
<input type="submit" name="submit" value="send comments"/>
</p>
</form>
</body>
</html>
Cheers,
MRb