Click here to Skip to main content
14,580,263 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



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.


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 = "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


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


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


About the Author

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 4:43
professionalMayurDighe21-Oct-16 4:43 
QuestionInjection Protection - Sanitising and Validating Pin
Mike-MadBadger20-Oct-16 18:35
MemberMike-MadBadger20-Oct-16 18:35 
AnswerRe: Injection Protection - Sanitising and Validating Pin
NewPast20-Oct-16 20:39
MemberNewPast20-Oct-16 20: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.

Posted 14 Oct 2016


8 bookmarked