Feedback/Contact Email Form

By Hawkee on Sep 04, 2004

This is a ready to go PHP script that can be pasted into a blank .php document. Run it through your webserver after changing $myemail to the value of your E-Mail address. It also sends the referring URL to help you know where the visitor was before he clicked to contact you.

<?php

// Change these two variables to meet your needs.

$myemail = 'myemail@domain.com';
$subject = 'Contact form Subject';

$op = $_POST[op];

if($op == 'contact')
{
    $name = stripslashes($_POST[name]);
    $email = stripslashes($_POST[email]);
    $text = stripslashes($_POST[text]);
    $referer = $_POST[referer];
    $remote_host = $_SERVER[REMOTE_ADDR];
    $server = $_SERVER[SERVER_NAME];
    $browser = $_SERVER[HTTP_USER_AGENT];

    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$",$email)) 
    { 
        $status = "We're sorry, but you've entered an incorrect email address.<br>";
    }
    if(!$name)
    {
        $status .= "Please enter your name.<br>";
    }
    if(!$text)
    {
        $status .= "Please enter a message.<br>";
    }

    if(!$status)
    {
        $header = "From: $email\r\nReply-To: $email\r\n";

        $message = "
            Name: $name
            Referer: $referer
            Site: $server
            Remote Host: $remote_host
            Remote Browser: $browser

            $text
        ";

        if(mail($myemail, $subject, $message, $header))
        {
            $status = "Thank you for your Feedback!!<br><br>";
        }
        else
        {
            $status = "There was a problem sending your feedback, please try again later.<br><br>";
        }

    }
    else
    {
        $status .= "<br>Please press <u>back</u> on your browser to resubmit.<br><br>";
    }
}    

// Now check the referer page and ensure it's a proper URL

$referer = $_SERVER[HTTP_REFERER];

if(!preg_match('#^http://[a-z0-9-]+.([a-z0-9-]+.)?[a-z]+#i', $referer))
{
    unset($referer);
}

?>

<?php print $status; ?>

<form method="post" action="<?php print $_SELF; ?>">
<input type="hidden" name="op" value="contact">
<input type="hidden" name="referer" value="<?php print $referer; ?>">
Name<br><input name="name" size="35" value=""><br>
E-mail address<br><input name="email" size="35" value=""><br>
<br>Message<br><textarea name="text" cols="50" rows="10"></textarea><br><br>
<input type="submit" value="Send message!">
</form>

Comments

Sign in to comment.
sunslayer   -  Jan 23, 2011

@J2011 add

<style>form,input{color:grey;}</style>
 Respond  
J2011   -  Jan 23, 2011

Hi, just wondering I want to use this on a site with a black background, is there a simple and easy way I can change the text colour to grey instead of black otherwise it won't be readable?

Thanks!

 Respond  
Stunningmesh   -  Sep 22, 2010

I've just received this form from someone. I was searching these kind of codes since last few days and thanks to my friend who just sent it to me. Also thanks to the person who has created it... I really worked.

 Respond  
wallver   -  Mar 25, 2010

i've found the solution here...

http://takien.com/513/how-to-fix-function-eregi-is-deprecated-in-php-5-3-0.php

Sorry for the disturb...

 Respond  
wallver   -  Mar 25, 2010

Deprecated: Function eregi() is deprecated in /directory/directory/directory/directory/directory/directory/filename.php on line 33

It seems that the function eregi() doesn't work with PHP 5.3

Any ideas?

if(!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$",$email))
{
$status = "We're sorry, but you've entered an incorrect email address.
";
}

 Respond  
gwidaz   -  Mar 20, 2010

i just creatig a website in free domain,because then i will be ready with my site,then i done this my web,i will upload in normal domain..now my hosting is byethost..maybe u are right..but can somehow this my email hide in source? or do u know maybe how hide all source? sorry for english,i hope u understand me :)

 Respond  
Hawkee   -  Mar 20, 2010

gwidaz, does your web host allow calls to the mail() function? I know some hosts don't allow this.

 Respond  
gwidaz   -  Mar 19, 2010

Dah....this script doesnt work..I dont get emails then i send from this sript..

 Respond  
edmarg   -  Jul 23, 2009

Got it! Sorry for being such a bother. I was looking for a reply withing the script. I don't want to sound redundant but thank you very much for all your help. This is a great script!
Appreciative,
Ed M

 Respond  
Hawkee   -  Jul 22, 2009

edmarg, the reply info should be in the headers. What email client are you using? You should just be able to hit the reply button at the top of the email.

 Respond  
edmarg   -  Jul 22, 2009

Hawkee, look at the info that is sent to my email in my last post to you. There is no reply button. should I build a new page and reinsert the script?

 Respond  
Hawkee   -  Jul 22, 2009

The email address should be in the From: field of the email, so all you need to do is hit the reply button and send your response.

 Respond  
edmarg   -  Jul 22, 2009

Thank you for your patients, Hawkee. This is what I see on my email:

        Name: test
        Referer: 
        Site: www.tranquilimages.com
        Remote Host: 76.88.229.88
        Remote Browser: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17

        testing

As you could see, I am not able to reply to the sender. Most of the time I would like to thank them for their feedback but are unable to. If you still have the patients to help me I would appreciate it very much.

 Respond  
Hawkee   -  Jul 22, 2009

edmarg, I just tested it and it seems to work properly. It accepted my email, so it should be contained in the email that gets sent to you.

 Respond  
edmarg   -  Jul 22, 2009

Hawkee, thank you very much for your reply. I am a novice at coding therefore need a little more help. I copied and pasted the script onto my contact form and only changed 'myemail@domain.com' to 'info@tranquilimages.com'. I left the ' marks in. Should I have changed anything else. Where is my "field name"? Thank you for your patients. You could look at my feed back form at http://www.tranquilimages.com/feedback/feedback[1].php

 Respond  
Hawkee   -  Jul 21, 2009

edmarg, just make sure your field name is 'email' and that you aren't clearing the value for $email somewhere in your code.

 Respond  
edmarg   -  Jul 21, 2009

Great script, except that I noticed that I do not get the email address of the person sending the message. Very often I would like to reply. What am I doing wrong? Thanks

 Respond  
Tyrone-ator   -  May 26, 2009

Awesome, no need to create a db or any other scripting required, tried it and it worked first time, definitely the simplest I have seen in a while.

 Respond  
Hawkee   -  Feb 21, 2009

toff, you just need to make sure you match up the input field names to the the fields in the PHP code. If you want to keep the .html extension you need to make sure your .htaccess is configured to parse .html documents as .php documents.

 Respond  
toff   -  Feb 21, 2009

hi guys, i already have a contact.html page, would it work with this php,, i dont want to change my contact page

 Respond  
darkarrow   -  Jan 13, 2009

when i do it it just shows the text that i put in it doesn't work :(

 Respond  
lostdeviant   -  Aug 14, 2008

Thank you

 Respond  
Hawkee   -  Aug 14, 2008

Just use this line instead when setting your $header:

$header = \"From: $email\\r\\nReply-To: $email\\r\\nContent-Type: text/html\\r\\n\";
 Respond  
lostdeviant   -  Aug 12, 2008

What would need to be changed to send HTML email messages?

 Respond  
aldog   -  Nov 07, 2007

This code is working great for me, and it was very simple to figure out. But I have one question. I just tested the form and it showed up in my email right away, but I was wondering what the \"Referer\" are was meant for (because it\'s blank for me) and when I try to reply to the person that sent it the reply to address is this

\"-To:\"@node4.c15

again, great script

 Respond  
Lyle   -  Aug 20, 2007

Answer for chopper: within the quotes, multiple emails seperated by comma works.

eg: \'aaaa@somewhere.com, bbb@somewhereelse.org, ccc@another.com\'

Question - How do we get the \'from\' header in these emails to not be blank? Can we specify a from address??

 Respond  
chopper72   -  Jul 17, 2007

Thanks for this nice, easy bit of php.

How can you add multiple email addresses for the $myemail ?
thanks

 Respond  
jmitton   -  Jul 15, 2007

Brilliant form. But can anyone tell me how to send the error text to a popup?

 Respond  
Hawkee   -  May 17, 2007

Why is it that none of my browsers will show the page?

I use RapidWeaver, i have pasted it into the html code.

But Firefox prompts me to download the PHP file, safari just displays the code as text.

Am i doing something wrong? =(

Please help!

rory, Are you running it on an Apache server with PHP? If you are just loading the .php document through explorer it won\'t run.

Hello, we are having trouble with your script because the e-mailing part doesnt work. We have tried the form out and we don\'t get any e-mails. We have tried over 4 email addresses! Plz help!

j-and-f, Maybe check with your web host and see if they allow you to send mail with PHP.

 Respond  
j-and-f123   -  May 13, 2007

Hello, we are having trouble with your script because the e-mailing part doesnt work. We have tried the form out and we don\'t get any e-mails. We have tried over 4 email addresses! Plz help!

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.