A simple PHP contact form

A simple PHP contact form


In this tutorial I will show you how to make a simple contact form in PHP.

On many sites such as this one, there is usually a tool which allows you to contact the owner of the website through an e-mail form. Here I will show you how to make a very simple mail form. For this tutorial it will help if you have a basic understanding of programming and html, although these are not necessary.

This tutorial will only require 2 files
sendmail.html and sendmail.php

As I usually do I will show the code for each of these files and then go through, break down and explain the code.


sendmail.html

<html>
<head>
<title>My PHP E-mail form</title>
</head>
<body>
<form action="sendmail.php" method="post" >
<input name="name" value="" type="text" size="30" value="Name"/>
<textarea rows="5" cols="5" name="message">Message</textarea>
<input type="submit" value="submit" />
</form>
</body>
</html>


There is nothing special about this code, it is simply a standard HTML page which shows a form with a name textbox and a message textbox, the important part is in the PHP code.


sendmail.php

<?php
$yourEmail = 'me@somesite.com';
$subject = 'My first e-mail';
$message = 'This e-mail was sent from: ' . $_POST["name"];
$message .= 'Message Content: ' . $_POST["message"];
$from = $yourEmail;

mail($yourEmail, $subject, $message, $from);
echo 'Message has been sent to ' . $yourEmail;
?>


This is a relatively simple piece of code and can easily be extended to work with other technologies. I will go through this code in 2 sections.


$yourEmail = 'me@somesite.com';
$subject = 'My first e-mail';
$message = 'This e-mail was sent from: ' . $_POST["name"];
$message .= 'Message Content: ' . $_POST["message"];
$from = $yourEmail;


This part of the code is simply assigning values to variables ready to be used, the line which needs to be edited is the top one, you need to change me@somesite.com to your email address. $subject is self explanatory, this will simply appear as the subject of the email. on the line after the $message variable you will see a string of text, this is simply to make you aware of what the content that follows contains, for example you will see:

This e-mail message was sent from: Matt


if I sent you a message :). Note that after the second message variable there is a fullstop before the '=' this is for sticking the string that follows to the original message variable to make something like:

This e-mail message was sent from: Matt
Message Content: Hello there!!


Now for the second part of code:

mail($yourEmail, $subject, $message, $from);
echo 'Message has been sent to ' . $yourEmail;


This first line sends the variables we made to a built-in php function which will send the email address to the address we specify. The second line just outputs a string which tells you the mail has been sent.

Have fun and I will shortly post a blog about security issues and how to overcome them with this kind of thing :)



Submit Comment

Comments

  1. ajai on October 05, 2009
    thanks 4 ur article

  2. discoph3v3r on August 15, 2009
    Nice tutorial