Top

Feedback/Contact Email Form


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  6.0 (of 1 scores)
Date Added  Sep 04, 2004
Last Updated  Jul 24, 2009
Tags  contact  contact form  email  feedback  form 

Introduction

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.

Grab the Code

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

  (32)  RSS
TeQh
Comments: 16
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Sep 4, 2004 1:57 am
nice man
Setementor
Comments: 2
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Sep 4, 2004 4:31 pm
Awesome.
AndrewMiller17
Comments: 31
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Oct 31, 2005 11:03 pm
I have a freewebs account and when I entered this code and tested it, it said abouve the form "This code is inoperational!". Don't know what that means.
AndrewMiller17
Comments: 31
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Nov 1, 2005 3:52 pm
Is anyone able to help me on this?
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Nov 2, 2005 9:21 pm
Hmm, that's odd. Does your host allow PHP scripts? Also, do they allow you to use the mail() function? You might want to talk to them about it.
AndrewMiller17
Comments: 31
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Nov 3, 2005 7:59 pm
Well, I investigated, and freewebs makes every acount wait 7 days before it can post any forms, to make sure people aren't just sing them for that.
markmck
Comments: 1
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Feb 1, 2007 1:52 am
Wonderfully simple, clean, easy to use and fit for purpose. Well done and thanks.
wizkidweb16
Comments: 6
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Mar 28, 2007 9:28 pm
For those who are having trouble, perhaps it's because you need to put the </form> tag at the end of the script.
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Mar 31, 2007 8:35 pm
Thanks wizkid! Don't know how that got cut off. I've corrected it.
roryflyguy
Comments: 2
 
PHP Snippet:  Feedback/Contact Email Form
Posted on May 12, 2007 7:27 am
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
j-and-f123
Comments: 1
 
PHP Snippet:  Feedback/Contact Email Form
Posted on May 13, 2007 4:40 am
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!
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on May 17, 2007 1:45 am
Quote:
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.

Quote:
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.
jmitton
Comments: 1
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 15, 2007 9:45 am
Brilliant form. But can anyone tell me how to send the error text to a popup?
chopper72
Comments: 1
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 17, 2007 8:33 am
Thanks for this nice, easy bit of php.

How can you add multiple email addresses for the $myemail ?
thanks
Lyle
Comments: 1
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Aug 20, 2007 9:20 am
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??
aldog
Comments: 2
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Nov 7, 2007 10:02 am
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
lostdeviant
Comments: 9
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Aug 12, 2008 8:56 pm
What would need to be changed to send HTML email messages?

Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Aug 14, 2008 2:07 pm
Just use this line instead when setting your $header:

Code:
$header = "From: $email\r\nReply-To: $email\r\nContent-Type: text/html\r\n";
lostdeviant
Comments: 9
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Aug 14, 2008 3:10 pm
Thank you
darkarrow
Comments: 24
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jan 13, 2009 8:33 am
when i do it it just shows the text that i put in it doesn't work :(
toff
Comments: 1
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Feb 21, 2009 7:32 am
hi guys, i already have a contact.html page, would it work with this php,, i dont want to change my contact page
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Feb 21, 2009 11:04 am
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.
Tyrone-ator
Comments: 1
 
PHP Snippet:  Feedback/Contact Email Form
Posted on May 26, 2009 9:45 am
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.
edmarg
Comments: 5
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 21, 2009 7:43 pm
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
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 21, 2009 8:03 pm
edmarg, just make sure your field name is 'email' and that you aren't clearing the value for $email somewhere in your code.
edmarg
Comments: 5
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 22, 2009 4:05 am
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
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 22, 2009 11:24 am
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.
edmarg
Comments: 5
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 22, 2009 5:07 pm
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.
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 22, 2009 6:38 pm
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.
edmarg
Comments: 5
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 22, 2009 8:16 pm
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?
Hawkee
Comments: 1,039
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 22, 2009 9:22 pm
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.
edmarg
Comments: 5
 
PHP Snippet:  Feedback/Contact Email Form
Posted on Jul 23, 2009 3:47 am
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

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom