PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP PHP Mail Form Tutorial
Your Ad Here

PHP Mail Form Tutorial


This script is widely used by many sites on the net. PHP provides an easy way to send mail via the function mail().

First of all, we ought to prepare the adequate fields to be filled by the user.

Ex: Name, E-Mail, Subject, Body

Now we start coding :

<HTML>
<head>
<title>My Mail Form</title>
</head>
<body>
<?

?>
</body>
</html>

Now we're going to work inside PHP braces ( <? ?>)

It's quite easy to write HTML code for a form. Just copy this:

<form method=POST name=form1>
<label>Name :</label><input name=name type=text><br>
<label>E-Mail :</label><input name=mail type=text><br>
<label>Subject :</label><input name=subject type=text><br>
<label>Body :</label><textarea name=body></textarea><br>
<input type=submit name=submit value=OK>
</form>

Since PHP is case sensitive, beware when writing field names (write low-case names).

Now, we've got to remember that our page is composed of "two pages": the first one contains the form and the second one sends the mail and displays "Thank You For using our mail form"

So, we use a condition :

if ($submit) ( as we named our button submit)

This HTML code should only appear when the user is not submitting his message. So, the code becomes:

<HTML>
<head>
<title>My Mail Form</title>
</head>
<body>
<?
if ($submit) {
//Here we add the send mail script
}else
//We added action=$PHP_SELF
//$PHPSELF means current page
//action normally contains the page to which the variables are sent
echo "<form method=POST name=form1 action='$PHP_SELF'>
<label>Name :</label><input name=name type=text><br>
<label>E-Mail :</label><input name=mail type=text><br>
<label>Subject :</label><input name=subject type=text><br>
<label>Body :</label><textarea name=body></textarea><br>
<input type=submit name=submit value=OK>
</form>";
?>
</body>
</html>

Now we have to add the script to send the mail.

Here it is:

<?
$from="From:$name<$mail>";
$tomail="mymail@dot.com";
if (mail($tomail,$subject,$body,$from)) echo "Thank You For using our mail form";
?>

So the final thorough script becomes :

<HTML>
<head>
<title>My Mail Form</title>
</head>
<body>
<?
if ($submit) {
//Here we add the send mail script
$from="From:$name<$mail>";
$tomail="mymail@dot.com";
if (mail($tomail,$subject,$body,$from)) echo "Thank You For using our mail form";
}else
//We added action=$PHP_SELF
//$PHPSELF means current page
//action normally contains the page to which the variables are sent
echo "<form method=POST name=form1 action='$PHP_SELF'>
<label>Name :</label><input name=name type=text><br>
<label>E-Mail :</label><input name=mail type=text><br>
<label>Subject :</label><input name=subject type=text><br>
<label>Body :</label><textarea name=body></textarea><br>
<input type=submit name=submit value=OK>
</form>";
?>
</body>
</html>



Author's URL: Kyscorp.com
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "PHP Mail Form Tutorial"

Only registered users can write comment

No comments yet...