Click here to Skip to main content
14,365,629 members

PHP Contact Us Script

Rate this:
5.00 (6 votes)
Please Sign up or sign in to vote.
5.00 (6 votes)
5 Jan 2017CPOL
PHP script use to send contactus form data to webmaster, It is simple and run without modification

Downloads

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

<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.

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:

<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:
    $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<br>';
                    $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 .= "<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

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<br>';               
    $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<br>';               
    $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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

NewPast
Lebanon Lebanon
---------------------------
There is a developer behind every piece of code!
DNA is too complex what about it!
No junk DNA; There is a functional role for noncoding DNA

Comments and Discussions

 
PraisePretty Good Efforts Pin
MayurDighe21-Oct-16 5:43
professionalMayurDighe21-Oct-16 5:43 
QuestionInjection Protection - Sanitising and Validating Pin
Mike-MadBadger20-Oct-16 19:35
memberMike-MadBadger20-Oct-16 19:35 
AnswerRe: Injection Protection - Sanitising and Validating Pin
NewPast20-Oct-16 21:39
groupNewPast20-Oct-16 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 14 Oct 2016

Stats

25.4K views
591 downloads
8 bookmarked