PHP Contact Us Script



Run out of box PHP contact us script, It does not need modification, it will detect the domain and send email containing the contact message to [email protected] Whatever fields are in your form will be detected and be sent with email.


Lots of contact us scripts are available over the internet. To use any other script, the modification of the PHP file is needed.

This script is very useful to those who do not know PHP and to the beginners of PHP.

Using the Code

Upload the script folder to your www root directory.

From Action

<form action="contact2us/send.php" method="POST">

Fields Names

Use from_email, from_name, subject, message and captcha as main fields’ names in your form.


If you don’t wish to use captcha, then no change is needed and the 1st line of code will be:
$captcha = false;
If you wish to use captcha, then change the 1st line of code to be:
$captcha = true;

To use captcha, include the following in your form:

<img src="contact2us/captcha_code_file.php?rand=<?php echo rand(); 
?>" id='captchaimg' ><br>

Enter the code above here : <input id="captcha" 

name="captcha" type="text"><br>

Thank you url

Put your own $thank_you_url in the 2nd line of the code.

What Does This Script Do?

  • Check the referrer page and stop the script if it is called directly:
                    die("This page can't be call directly");
  • Validate user email and user name to prevent injecting wrong command in the header parameter of the mail function:
    if(!$from_email) $from_email = "[email protected]$domain";
    if (!filter_var($from_email, FILTER_VALIDATE_EMAIL)) {
                    $Err .= 'Invalid email format<br>';
                    $from_email = "[email protected]$domain";
  • Validate subject and encode it if needed to prevent send failure:
    if ($subject && !preg_match('/^[A-Za-z ]+$/',$subject)){
                    $subject = "=?UTF-8?B?".base64_encode($subject)."?=";
  • Store captcha in session and compare it with variable
  • Seek all posted variables
    foreach ($_POST as $key => $value)
        $value = htmlspecialchars($value);
        $message_html .= "<h2>$key</h2><p>$value</p>";
  • Send the message in html utf-8 format to be compatible with most languages
  • Redirect to thank you URL
    header('Location: '. $thank_you_url);

PHP Mailing Technique

There are many mailing technique in PHP; PEAR Mail, PHP Mailer and mail function. However we just use mail function as it is old, common and simple.

PHP Email Validation


Remove all illegal characters from an email address:

$from_email = filter_var($from_email, FILTER_SANITIZE_EMAIL);


Check if the variable $email is a valid email address:

if (!filter_var($from_email, FILTER_VALIDATE_EMAIL)) {                    
    $Err .= 'Invalid email format<br>';               
    $from_email = "[email protected]$domain";

Validate email in PHP using regular expression:

$pattern = '/^[\w.-][email protected][\w.-]+\.[A-Za-z]{2,6}$/';
if(!preg_match($pattern, $from_email)){ 
    $Err .= 'Invalid email format<br>';               
    $from_email = "[email protected]$domain";

What is the Next Step?

Setting the max email could be sent for a single IP per hour.

If you have any suggestion to this section or to improve the script; please write it in the comments to be included in the next version.


You might also like
Leave A Reply

Your email address will not be published.