A walkthrough showing how to creating your own send mail php script for a contact form on your website.
By Richard Ye - yerich.net
Many professional web developers charge a fair amount of money to put a contact form onto your website. These forms send an e-mail to the website owner without giving away his/her e-mail address, or provide a simpler interface for sending an e-mail. This tutorial will show you how to make one.
The first step is to create a file that contains the form that has the fields. This can be an HTML or PHP file. The information in the fields will be passed to the PHP script, which will actually send the e-mail.
First, the form tag, whose action attribute tells the browser to send the information to the sendemail.php file, and the method attribute tells the browser to use the "post" method:
Code:
------------------------------------------------------------------------
<form action="sendemail.php" method="post">
------------------------------------------------------------------------
Next comes the fields itself. The "input" tags have the attribute "type" as text to display them as text. The "textarea" tag signifies a textarea. Note how each tag has a "name" attribute; this will be used to identify the fields in the php script.
There are also "label" tags to detonate the labels of each field.
Code:
------------------------------------------------------------------------
<label for="name">Your Name</label>
<input type="text" name="name" />
<label for="email">Your E-Mail Address</label>
<input type="text" name="email" />
<label for="message">Message</label>
<textarea name="message"></textarea>
------------------------------------------------------------------------
Now end the form:
Code:
------------------------------------------------------------------------
</form>
------------------------------------------------------------------------
All Together:
Code:
------------------------------------------------------------------------
<form action="sendemail.php" method="post">
<label for="name">Your Name</label>
<input type="text" name="name" />
<label for="email">Your E-Mail Address</label>
<input type="text" name="email" />
<label for="message">Message</label>
<textarea name="message"></textarea>
</form>
------------------------------------------------------------------------
Now for the fun part - making the PHP script that powers it all. Remember the action attribute of the HTML form? Well, you're going to have to create that file - sendemail.php - in the same directory as your form. This file must be a PHP file, or else your server will not excecute the code (there are exceptions, but this is the rule of thumb).
Put the following content into your file:
Code:
------------------------------------------------------------------------
<?php
// You'll need to change this variable to whatever e-mail address
// you want PHP to send it to.
$dest = "[email protected]";
// Find the variables from the HTML form, and make them into
// PHP variables. Note how the name attribute of each tag
// is reflected in the variable.
$email = $_POST['email']; //E-Mail Address
$name = $_POST['name']; //Name
$message = $_POST['message']; //Message
// PHP sometimes automatically adds slashes before quotes;
// Use stripslashes() to take them out
$email = stripslashes($email);
$name = stripslashes($name);
$message = stripslashes($message);
// Set the subject of the e-mail
$subject = "Contact Form Message from $name";
// Next, we want to validate the fields.
// If a field is blank, return an error
if(!$message||!$name||!$email) {
echo("All fields are required. You left one blank.");
exit;
}
// If the e-mail address is not vaild, return an error
// This is rather complicated, basically it just checks for the
// presence of a "@" and a dot
elseif((!strstr($email,"@") || !strstr($email,"."))) {
echo("You haven't entered a valid e-mail address!");
exit;
}
// E-Mails have headers; metadata that is sent describing the
// content of the e-mail. For our purposes, we shall include
// one header - the sender's e-mail address.
$headers = "From: $email";
// The form is valid, now send out the e-mail.
// This uses the mail() function. It takes three main variables:
// $to - the recipent
// $subject - the subject
// $content - the content
// $headers - the headers
if(mail($dest, $subject, $message, $headers))
echo("Message Sent Sucessfully");
else
echo("Error encountered");
?>
------------------------------------------------------------------------
This script is not secure. There is nothing stopping a robot from sending thosands of e-mails to your inbox using this form. This is only meant as a starting point. Possible next steps include:
The rest is up to you!
Richard Ye is the owner of Yerich.net.