PHP contact form to email account
Posted in PHP | Posted on 20-01-2010-05-2008
In this tutorial we will learn how to use a basic HTML form with a PHP script. We will be working on a simple contact form that can be used on your website.
The form will take the user’s input, and output the data to an email address and a “thank you” page.
Here is the HTML form:
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″ />
<title>Contact Us</title>
</head>
<body>
<form action=”http://www.yoursite.com/contactform.php” method=”post”>
Name:
<br />
<input type=”text” name=”name” />
<br /><br />
Email Address:
<br />
<input type=”text” name=”email” />
<br /><br />
Comments:
<br />
<textarea rows=”10″ cols=”20″ name=”comments”> </textarea>
<br /><br />
<input type=”submit” value=”send” />
</form>
</body>
</html>
In the form tag, change the action value to your domain name. The PHP form will be called contactform.php
Our form consists of a name field, an email field, and a large text field.
Here is the php file:
<?php
$name = $_POST[name];
$email = $_POST[email];
$comments = $_POST[comments];
$to = “youremail@yoursite.com“;
$subject = “Comments From Website”;
$input_name = “Name: $name\n\n”;
$input_email = “Email: $email\n\n”;
$input_comments = $comments;
$message = $input_name.$input_email.$input_comments;
mail($to,$subject,$message);
?>
<!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”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″ />
<title>Comments received</title>
</head>
<body>
<h2>Your comments are appreciated.</h2>
<p><strong><?php echo($name); ?></strong>, we value your comments. We will see that we promptly reply to you at the following email address: <?php echo($email); ?></strong></p>
Let’s dissect this code piece by piece.
$email = $_POST[email];
$comments = $_POST[comments];
These lines of code is where we will get the input data from the form. We create our variables. The variable $name will be equal to the name input value from the HTML form. The email and comments variables work the same way.
$subject = “Comments From Website”;
Here we declare two more variables. The $to variable value will be your email address. I would suggest to avoid using free email accounts such as yahoo or hotmail. Free email account servers will probably block you after so many attempts. They will think your account is getting flooded with junk mail. Use the email account that came with your host server. The $subject variable can be anything you like. This is the title you will see when you check your inbox.
$input_email = “Email: $email\n\n”;
$input_comments = $comments;
These variables are not necessary for the script to work. In this example, we use them to format the data that is being sent to our email address. A thing to note, the \n creates a new line.
Here we join the the variables into one variable named $message. This is referred to as concatenation.
This is the line of code that will actually send the data to our email address.
When you open your email, based on how we formatted the data, it should look similar to this:
Finally we then provide an output page to let the user know that their information was received. The output page for this example should look like this:
That is the basic fundamentals of receiving data from a user and manipulating it with a server side script. I must mention this script is not the end all be all. For instance, it would be wise to implement validation. Validation is used to keep users from entering invalid data into the fields. Say you have a field that asks for a phone number. Validation would not allow the user to submit the form if for instance, they entered a @ character in the phone number field.
Nonetheless, you can start off with a script as simple as this one and customize it to your needs.


