PHP Contact form script
— 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.
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>
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!




The script looks rather good, thanks for sharing! keep up the good work!!
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!
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:
Hope that helps,
Cheers!
Regards