getButterfly Logo getButterfly

The last tutorial was about a PHP/MySQL counter for a client. The same client requested a simple contact form, with no AJAX, or other options. So, I used the PHP mail() function, wrapped in a simple XHTML form.

First, we create the XHTML form (contact.php):

[xhtml]<h1>Simple Contact Form</h1>
<form name=”form1″ method=”post” action=”send_contact.php”>
<p><input name=”subject” type=”text” id=”subject” size=”50″ /> <label for=”subject”>Subject</label></p>
<p><textarea name=”detail” cols=”50″ rows=”4″ id=”detail”></textarea> <label for=”detail”>Detail</label></p>
<p><input name=”name” type=”text” id=”name” size=”50″ /> <label for=”name”>Name</label></p>
<p><input name=”customer_mail” type=”text” id=”customer_mail” size=”50″ /> <label for=”customer_mail”>Email</label></p>
<p><input type=”submit” name=”Submit” value=”Submit” /> <input type=”reset” name=”Submit2″ value=”Reset” /></p>

Next, we create the mail sending engine (send_contact.php):

$subject =”$subject”;
$header=”from: $name <$mail_from>”;

// Your email address
$to =’’;

// If message was successfully sent, display a message
if($send_contact) {
echo “Thank you for your interest. We will answer as soon as possible.”;
else {
echo “Error: The email was not sent. Check your server.”;

That is all. You can include the contact.php file into your web site design.

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

6 thoughts on “Simple PHP/MySQL Contact Form Tutorial

  1. That’s super simple :) No verification of any kind anywhere. Is this good to directly use these variables without any escaping? This kind of code result into cross site scripting vulnerabilities (remember mikeyy on twitter?)

    A couple of points you might want to consider:

    1. You should include the USER-AGENT IP address in the email.

  2. This is just an example of contact form framework. Additional checks should be placed in the form.

  3. cool post, that was help me solve my problem, of course works perfectly for me! many thanks
    .-= maxomen’s last blog ..SEO Indexed Search Engine =-.

  4. hey thanks for sharing this. I have been having a little difficulty with a contact form so this might just come in handy :)

Leave a Reply

Your email address will not be published. Required fields are marked *