PHP Contact form script

This script has been updated and improved. Click here to go to the newer version.

— This is a php script to manage contact forms. Its very simple but effective, any critics or comments are welcome. It consist of two files, sendmail.php and contact.php. The email sent is in plain text format, but you could easily change that.

→ You can go ahead and download the script here

Sendmail.php

This is the script that handles the form/s.

<?php

# ------------------------------------------------------------

// PHP CONTACT FORM SCRIPT v1.0

// By Matt Varone | http://www.mattvarone.com

# ------------------------------------------------------------

# ------------------------------------------------------------

// General Configuration

# ------------------------------------------------------------

// Name of the website.

$szFrom = "My Website";

// Your email, (the recipient email address).

$szRecipient = "email@somedomain.com";

// Website email address ( Sender's email address ).

$szFromEmail = "email@somedomain.com";

/* Title comes from a hidden input in the form. This gives

the possibility to set up more than one form and use this same script.*/

$szTitle = $_POST['title'];

// This sets up the email subject.

$szSubject = $szFrom.": ".$szTitle;

/* All input fields coming from the form/s should go here.

The first value is the name attribute used and the second

value on the right the name wanted to display

on the final email.*/

$aPosted = array(

"name" => "Name",

"telephone" => "Telephone Number",

"email" => "Email Address",

"comments" => "Comments, Critics and Suggestions",

// add here your fields

);

# ------------------------------------------------------------

// Email Content

# ------------------------------------------------------------

$szEmailContent.= $szSubject."\n";

$szEmailContent.= "------------------------------------------------------------\n\n";

$szLast = "";

foreach($aPosted as $value => $szReal)

{

if(isset($_POST[$value]))

{

if($szLast != $szReal)

{

$szEmailContent.= $szReal.":";

$szLast = $szReal;

};

$szEmailContent.= " ".$_POST[$value]."\n\n";

};

};

# ------------------------------------------------------------

// Declare Email headers

# ------------------------------------------------------------

$szHeaders = 'From: ' . $szFromEmail . "\r\n" . 'Reply-To: ' . $szFromEmail . "\r\n" . 'X-Mailer: PHP/' . phpversion();

# ------------------------------------------------------------

// Output

# ------------------------------------------------------------

if( mail( $szRecipient, $szSubject, $szEmailContent, $szHeaders, '-f' . $szFromEmail ) )

// go to previous page ($lasturl) with message=1 (should be a succes message).

header( 'Location: http://' . $_SERVER['HTTP_HOST']  . html_entity_decode( $_POST['lasturl'] ) . '?message=1' );

else

// go to previous page ($lasturl) with message=2 (should be a failure message).

header( 'Location: http://' . $_SERVER['HTTP_HOST']  . html_entity_decode( $_POST['lasturl'] ) . '?message=2' );

?>

contact.php

Simple form as an example:

<h1>Contact Form Example</h1>

<form action="sendmail.php" method="post">
<input type="hidden" name="title" value="My Contact Form Title"/>
<input type="hidden" name="lasturl" value="<?php echo ($_SERVER['REQUEST_URI']); // this is important ?>"/>

<p><label>Name:</label><br/>
<input type="text" name="name" value=""/>
</p>

<p><label>Telephone Number:</label><br/>
<input type="text" name="telephone" value=""/>
</p>

<p><label>Email:</label><br/>
<input type="text" name="email" value=""/>
</p>

<p><label>Comments:</label><br/>
<textarea name="comments" rows="8" cols="40"></textarea>
</p>

<p><input type="submit" value="Continue"></p>

</form>
→ Go ahead and download the script here

I think that the overall concept of the script is pretty simple but let me know if you have any questions. Note that currently this form don’t do any kind of input validation, you should add your own or wait for a new update. I Hope it helps someone out there. Cheers!

Tags: , , ,

Tuesday, September 16th, 2008 Web Design & Development

→ If you find this post useful please consider inviting me a cup of tea :) Thanks!

7 Responses to “PHP Contact form script”

  1. The script looks rather good, thanks for sharing! keep up the good work!!

  2. Sam on September 20, 2008.
  3. Hi man,

    Nice simple script. How would one go about having a number of different recipients that I want to be able to select for the user (perhaps through hiding the field)?

    For example, say the website is split up into sales and marketing. If someone is on the sales page, I want to send the e-mail to the sales e-mail address, but if they are on the marketing page, I want it to be sent to that e-mail contact. Does that make sense?

    I still have yet to come accross a solution and was hoping you could help. Thanks!

  4. Ryan on October 19, 2008.
  5. Hi Ryan,
    Thanks for stopping by. There are multiples ways to achieve what you want, a simple way would be to add a hidden field in your html file with the section name and then run a conditional check in sendmail.php to see from wich page the user sent the form, and from there send the email to the correct recipient.

    HTML

    <input type="hidden" name="destination" value="marketing" id="destination"/>
    or
    <input type="hidden" name="destination" value="sales" id="destination"/>

    PHP

    replace in sendmail.php the line 29 ( $szRecipient = "email@somedomain.com"; ) with:

    <?php
    if ( $_POST['destination'] )
    {
    	switch ( $_POST['destination'] )
    	{
    		case 'marketing':
    			$szRecipient = "marketing@somedomain.com"
    			break;
    
    		case 'sales':
    			$szRecipient = "sales@somedomain.com"
    			break;
    
    		default:
    			$szRecipient = "email@somedomain.com"
    			break;
    	}
    }
    else
    {
    	die('Please specify a destination.')
    }
    ?>

    Hope that helps,
    Cheers!

  6. Matt on October 19, 2008.
  7. Regards

  8. Bastien on November 21, 2009.
  9. hello fantastic website yea nice work our review blog will soon be adding reviews on blogs and add them to our websites as the top best 25 websites to visit we also do reviews on product recalls all types of reviews

  10. BARBARA LEVINE on March 14, 2010.
  11. Hi there – nice, simple script! thanks for the work! I’m trying to use it on a form that has a couple of dropdown lists, and I can’t figure out how to have the selections passed. i’ve managed to add a few radio buttons and additional text fields, but the script totally ignores the option info. Any chance you can spare a sec to let me know what I’m missing? Thanks!

  12. shelly d on May 20, 2010.

Leave a Reply

About Me

Matt Varone - Matias Varone - sksmatt
HI there,

I'm a freelance creative web developer, UI designer and hobbyist musician.

Twitter Status

Flickr Gallery

    Blue WallClutter drawerCS4 Replacement iconsCS4 Replacement icons