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>


Related Articles

  • Form Mail via CDONTS
    ASP gives you the power to send mail using the CDONTS mail object. This script will send an email from a user to whichever email address you choose...
  • E-mail form using HTML and PHP
    Sometimes its a nice touch to include an e-mail feedback form on your contact page or elsewhere on your web site. Lets create an e-mail form using html and a simple php script to send it along.
  • How to make a simple form mailer with PHP
    As you may be well aware, displaying your e-mail address on your website can lead to e-mail harvesters picking up your address and adding it to hundreds or even thousands of lists used by spammers...
  • Guide to Logo Design
    This excellent tutorial entitled Guide to Logo Design is a quick overview of concept design and the visual design of a logo. Any logo must be easily recognized and appreciated.
  • PHP Feedback Form: Advanced
    The problem with my basic PHP Mail Form tutorial is that it offers no protection from spam and meta tag injection. This is fine if you have a very low-traffic site not listed at major search engines, or have a private website, but bigger sites need more.
  • Do not post to FFA's (Free For All Sites) & Link Farms
    In this tutorial Martin Lemieux presents a compelling case for why you should not use Link Farms and FFAs to promote your business.
  • Form Email
    Learn to send mail with the php mail(); function.
  • Contact Form
    Here I will explain to you how you can create a simple contact form. The form will collect data from the visitor and send it to any email address you please. The only requirement for this tutorial is for you to have .php enabled on your server. If you dont want to read through the whole tutorial a....
  • Form Validation using PHP
    Today Ill take it up a level and re-write that script using the if, else conditional and empty() function to include form validation.
  • Create a Contact Page with HTML and PHP
    I have made a tutorial about how to install EasyPhp and now i come with an example. Today we learn how to create a Contact Page in HTML and PHP very easy! Download the next files to exercise on them:

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.