-
Notifications
You must be signed in to change notification settings - Fork 2
Home
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.
Upload the script folder to your www root directory.
$captcha = false;
$captcha = true;
To use captcha, include the following in your form:
Enter the code above here :
Put your own $thank_you_url
in the 2nd line of the code.
-
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);
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.
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
'; $from_email = "web_page@$domain"; }
$pattern = '/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/';
if(!preg_match($pattern, $from_email)){
$Err .= 'Invalid email format';
$from_email = "web_page@$domain";
}
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.