Skip to content
NewPast edited this page Jan 4, 2017 · 1 revision

Introduction

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 info@yourdomain. Whatever fields are in your form will be detected and be sent with email.

Background

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

Fields Names

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

Captcha

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:

Enter the code above here : 

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:

    $REFERER = $_SERVER['HTTP_REFERER'];
    if(!preg_match("@^http:\/\/(www\.)?$domain\/@",$REFERER)){
                    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 = "web_page@$domain";
    if (!filter_var($from_email, FILTER_VALIDATE_EMAIL)) {
                    $Err .= 'Invalid email format';
                    $from_email = "web_page@$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 .= "$key$value";
    }
  • 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

PHP FILTER_SANITIZE_EMAIL Filter

Remove all illegal characters from an email address:

$from_email = filter_var($from_email, FILTER_SANITIZE_EMAIL);

PHP FILTER_VALIDATE_EMAIL Filter

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

if (!filter_var($from_email, FILTER_VALIDATE_EMAIL)) {                    
    $Err .= 'Invalid email format
'; $from_email = "web_page@$domain"; }

Validate email in PHP using regular expression:

$pattern = '/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/';
if(!preg_match($pattern, $from_email)){ 
    $Err .= 'Invalid email format';               
    $from_email = "web_page@$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.