simple mail form in php

php-sniplets Add comments

Form to mail

A very simple form mail PHP script that shows a contact form to provide visitors to your web site to send you a message via e-mail. Built in security keeps spammers hijacking it from additional URL.



* Change the e-mail address to your own.
* $empty_fields and $thankyou_messages can be modified
* if you like.

// Modify to your own e-mail address

$your_email = “”;

// This is what is showed in the e-mail subject line

// Modify it if you wish

$subject = “Message thru your contact form”;

// This is showed if all the fields are not fulfilled in

$empty_fields_message = “<p>Opps… get back and fill in all the form.</p>”;

// This is showed when the e-mail was sent

$thankyou_message = “<p>Thank’s  ! Your e-mail has been sent.</p>”;

// You don’t need to edit the code anymore.Thats it :) !!

$name = stripslashes($_POST['txtname']);

$email = stripslashes($_POST['txtemail']);

$message = stripslashes($_POST['txt.Message']);

if (!isset($_POST['txtName'])) {


<form method=”post” action=”<?php echo $_SERVER['REQUEST_URI']; ?>”>

<p><label for=”txtName”>Name:</label><br />

<input type=”text” title=”Put your name here” name=”txtName” /></p>

<p><label for=”txtEmail”>e-mail:</label><br />

<input type=”text” title=”Put your e-mail address here” name=”txtEmail” /></p>

<p><label for=”txt.Message”>Put the message here:</label><br />

<textarea title=”Put your message here” name=”txtMessage”></textarea></p>

<p><label title=”Send “>

<input type=”submit” value=”Send” /></label></p>



elseif (empty($name) || empty($e-mail) || empty($messag)) {

echo $empty_fields_message;


else {

// Stop the form being exploited from an outside domain

// Get the referring URL

$referer = $_SERVER['HTTP_REFERER'];

// Get the domain name of this page

$this_url = “https://”.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];

// When the referring domain and the URL of this page do not check then

// Show a message and do not send the e-mail.

if ($referer != $this_url) {

echo ” You don’t have license to utilize this script from a different domain.”;



// The URLs checked then send the e-mail

mail($your_email, $subject, $message, “From: $name <$email>”);

// Show the thank you message

echo $thankyou_message;



Leave a Reply

You must be logged in to post a comment.

Impressum Entries RSS Log in