PHP tutorial: simple contact form using the mail() function

October 4th, 2009 by JS Leave a reply »

Here is an extremely simple way to create a contact form with PHP and the mail() function.

Copy the code above and keep reading..

<?
if ($_POST){
$to = “youremail@gmail.com”;
$subject = “YOUR SUBJECT”;
$email = $_REQUEST["email"];
$name = $_REQUEST["name"];
$message = “Name: “.$name.”\r\n”. “Email: “.$email.”\r\n”. “Message: “.$_REQUEST["message"];
$headers = “From: $email”; mail($to, $subject, $message, $headers);
echo “Thanks for submitting.”;
}
else
{
?>
<html>

<head>

<title>Contact Us</title>

<body>
<table>

<form action=’email.php’ method=’post’>

<tr>

<td>

Your Name:

</td>

<td><input type=’text’ name=’name’></td>

</tr>

<tr>

<td> Your Email: </td>

<td><input type=’text’ name=’email’></td>

</tr>

<tr>

<td> Message: </td>

<td><textarea name=’message’></textarea> </td>

</tr>

<tr><td colspan=”2″><input type=’submit’ value=’Send
your comments’> </td></tr>

</form>

</table>

</body>

</html>

<?php } ?>

Now create a new text file and name it email.php.
Paste the above code into that file and edit the e-mail address and the rest of the details.
Now save it and upload it to your web server.
Point your browser to www.yoursite.com/email.php.
Complete the form and press the send button.
Check your e-mail.

Enjoy!

P.S. Leave a comment if you find this script useful and I’ll add more.

Share and Enjoy:
  • Digg
  • Yahoo! Buzz
  • Google Bookmarks
  • del.icio.us
  • Live
  • Reddit
  • StumbleUpon
  • Propeller
  • Facebook
  • Mixx
  • LinkedIn
  • Slashdot
  • Technorati
  • Twitter

5 comments

  1. Joseph says:

    Awesome! This script is working like a charm. Thanks for sharing it.

  2. Matthias says:

    …a short hint, that I have to have a hosting package that really prepare php commands would be helpful.

  3. Steve says:

    You have no form validating what so ever?
    I know you said it’s a simple contact form but your not even checking to see if inputs are empty or if email is valid.

    example of empty field should be a minimum
    $sender = $_POST['sender'];
    $email1 = $_POST['mail1'];
    $email2 = $_POST['mail2'];
    $text = $_POST['text'];

    if (!$sender || !$email1 || !$email2 || !$text){
    echo “You forgot to enter a field.”;
    die;
    }

    and you should be validating the email as well. Although your not inputting into a db you should verify it’s real

  4. JS says:

    @Steve Thank you for your feedback.
    You are right, it is a good idea to check if they completed what we need but, as I mentioned, this code is really basic.

    If you want to share an advanced mail form, you can e-mail it to me and I’ll add it as a new post.

  5. Des says:

    This script has come along at the right time. I’m in the process of adding a form to one of client’s websites. Thanks for this!

    Des.

Leave a Reply

Web Development blogs Top Internet blogs Blog for webmasters. Tips, tutorials, reviews and a lot more. BlogRankers.com TopOfBlogs Sedo - Buy and Sell Domain Names and Websites project info: jspromotion.com Statistics for project jspromotion.com etracker web controlling instead of log file analysis
This website might be for sale. Click here for details.
Close it.