logo

PERFECT

PHP Email Receive Form Electronic Content Text

logo Fork me on GitHub

Very basic PHP script

PERFECT is a small and efficient PHP script that processes an HTML web form and emails the information to a predefined recipient.

Web hosting service providers typically include PHP running on Linux for their basic hosting and reseller packages.  Using PHP's built-in email function, you can capture user input with just a few simple lines of code.

The PERFECT project focuses on simplicity and reusability rather than on complex features.  Think of the project as the "Hello, World" version of PHP web form processing. PERFECT is open source under the MIT License or the WTFPL (your choice) and is free for anyone to use.

Highlights

Automatic field detection:
Searches the form (POST data) to find all input fields so that you can modify the HTML web form without having to change the PHP script.
Host data:
Message includes user's host information.
Hidden email address:
Email address used by the script is hidden for your protection.
Safe email content:
Content of message is stripped of special characters in case someone attempts to submit malicious content.
Easy configuration:
PERFECT is easily configured by editing the setup variables at the top of the script.

The code

perfect.php View on

            <?php
//////////////////////////////////////////////////////////
// PERFECT PHP                                          //
// v2.0.4 (October 22, 2017)                            //
// Process a web form to extract the user input and     //
//    then email the data to a predefined recipient.    //
// MIT or WTFPL (your choice)                           //
// http://centerkey.com/php                             //
//////////////////////////////////////////////////////////

// Configuration settings
$sendFrom =    "Form Feedback <feedback@yourdomain.com>";
$sendTo =      "feedback@yourdomain.com";
$subjectLine = "Feedback Submission";
$thanksUrl =   "thanks.html";  //confirmation page
$body =        "";  //first line of message body

// Build message body from web form input
foreach ($_POST as $field=>$value)
   $body .= "\n$field: $value";
$body .= "\n\n" . @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
$body = htmlspecialchars($body, ENT_NOQUOTES);  //make safe

// Send email and direct browser to confirmation page
mail($sendTo, $subjectLine, $body, "From: $sendFrom");
header("Location: $thanksUrl");
?>
         
Sample HTML web form View on

            <form class=perfect>
   <h2>Send us a message</h2>
   <label>
      <span>Message:</span>
      <textarea name=message placeholder="Enter your message"></textarea>
   </label>
   <label>
      <span>Name:</span>
      <input name=name placeholder="Enter your name">
   </label>
   <label>
      <span>Email:</span>
      <input name=email type=email placeholder="Enter your email">
   </label>
   <p>
      <span>Powered by <a href=http://centerkey.com/php>PERFECT</a></span>
      <button type=submit>Send</button>
   </p>
</form>

<script src=https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js></script>
<script>
   $('form.perfect').attr({ method: 'post', action: 'perfect.php' });
</script>
         

The result

If you add the styling from perfect.css , the form looks like:

Send us a message

Powered by PERFECT

When a visitor to your website clicks the "Send" button, you'll receive an email that looks something like:

Sample feedback message

            From: Form Feedback <feedback@yourdomain.com>
            Subject: Feedback Submission
            To: feedback@yourdomain.com
            Date: Sun, 20 Nov 2013 17:07:26 -0400

            message: I wish your website offered search by brand.
            name: Susie
            email: sample@gmail.com

            3.B56A7F.dsl.dynamic.sonic.net
         

Instructions

To use PERFECT on your website:
  1. Download the PERFECT script (perfect.php) and move in your website files.
  2. Edit the Configuration settings in the script to send email to yourself.
  3. Add the example CSS and HTML to your website or create your custom HTML and CSS.
  4. Upload your website files to your web server.

To keep things simple, put the "perfect.php" file into the same folder as your HTML file.  Check out the Troubleshooting Tips and FAQ if you want more information or encounter any problems, such as being hit by spam.

Random

"Just what I needed.  Good to know that you are out there.  Cheers" B.D., February 1, 2010

"thanks a million, you are da bomb...." E.M., October 19, 2009

"Dude!  I spent months trying to get so many damn forms to work...  Register Global issues, re-direct page issues etc...  This script is legendary!  Thank you!" A., October 2, 2009

"Thanks pal, tried a few and none of them worked but this one was brilliant.  Thanks again!" J., September 30, 2009

"This is easy and great - the best and most intuitive form script I ever used.  Thank you!" D., September 25, 2009

"This is ideal!  There's dozens out there but most are bloated, feature-strangled, and a whole lot less flexible than the programmer imagined them to be.  5 peppers at hotscripts." J., May 20, 2009

"GENIUSSSSSS!!!!!  Just a PERFECT script" D.T., April 8, 2009

"This is awesome saves so much time." M.C., April 7, 2009

"Many thanks.  I never thought to solve my message problems with a so short but very efficient script." A.B., March 7, 2009

"Very easy and straight foward Tutorial.  Thanks alot" C., February 27, 2009

"awesome, this actually works and is simple to understand unlike others...  well done and thanks for letting me have this for free, [people like you save me alot of head scratching. thanks again" R., February 16, 2009

"Thanks for this script!  Works great for me its simple but it does the job!  Thank You!" R., February 10, 2009

"That is fantastic mate.  1 go no problem.  This website is highly recomended" A., February 8, 2009

"FANTASTIC SCRIPT!  I love you!  thank you :-)  long live to you" V., January 14, 2009

More...

"This is the best Form to Email script I have found simple but very good!" C.E., December 29, 2008

"Merci beaucoup pour votre superbe script.  Fonctionne très bien et facile à utiliser"
("Thank you very much for your wonderful script.  Works great and easy to use" — Google Translator) R. (Switzerland), November 12, 2008

"What a find! I was going mad with frustration until I found your marvellous form.  Thank you so much! (from Perth Australia).  Ps. no need to reply..." C., November 9, 2008

"thanx for your free script.  Hope you will do the same work in future and give me good time to spend with you website and steal your wonderscript" S., November 8, 2008

"This Script Rocks!" C.S., October 31, 2008

"Prachtig simpel en het werkt ook nog.  Dagen werk in zoeken met ingewikkelde scripts.  Deze in een half uurtje klaar!"
("Beautifully simple and it works also.  Now work with complex search scripts.  This ready in half an hour!" — Google Translator) L.V., October 18, 2008

"It works and it's great!  I tried so many scripts before and they didn't work at all.  Thank you very much!" Y., September 19, 2008

"easiest script i ever used" Y.G., September 18, 2008

"thanks you!  this is the best easy to use script!  great job!!!" G.G., September 4, 2008

"Dude thanks so much for this code, its very functional and works perfectly, I hope you have a good day, cause you just make me have one =)" A.R.J., August 22, 2008

"Well Done, easy, fast and very clear to understand it WORKS!!  finally :)" M., August 16, 2008

"A very PERFECT Email form script.  Thanks alot." K., July 4, 2008

"Thank you so much for this wonderful script.  You are tha best!!!" Q.D., February 5, 2008

"You're awsome!  Thanks a lot for an easy-to-use script." B.V., January 24, 2008

"My first 'successful' email form script after trying many.  Thanks so much." J.B.J.R, November 4, 2007

"You are a god!!!!!!  Thanks man i have been looking all over for a easy script like this" M., October 22, 2007

"THANK YOU for such a SIMPLE script!  None of this 100+ feature stuff.  I love it -- thanks!" S., October 3, 2007

"Wow... it is perfect!  I have been searching something so simple and efficient for a loooong time!  Now forms are fun.  Thank you!  :)" A., September 14, 2007

"It is a simple form, exactly what I need.  Thanks!" S.M., July 18, 2007

"Your PERFECT email script is the best script I have EVER used!  It made my life so much easier!!!!  I was struggling with an old Perl script and finally gave up and searched for something easier to use.  Then I came across this script and I'm amazed at its simplicity!  Who knew it could be so elegantly easy?????  Thank YOU!!!!!!!!!!!!!!!!!!!!" D., July 16, 2007

"I'm using you php email script thank you so much It work perfectly." S.G., June 19, 2007

"thanks so much for a great script!" M., June 17, 2007

"Super, great, fantastic, fab, all words that sprang to mind as soon as this worked.  And it worked first time!  Great stuff." C., May 2, 2007

"Once of the most simplest & easiest & most useable scripts have come across after loads of time spent on net" S., November 29, 2006

"Eazy as pie" C.B., October 16, 2006

"how easy was that!  Brilliant  thanks very much" M.R., October 2, 2006

"Wonderfullly good.  The simplest structured script for a form mailer.  Wondefull  Thank you very much" S.K.T.K., September 28, 2006

"Your great little script does exactly what I wanted - it is really neat.  Thanks" A.M., August 14, 2006

"After looking at a dozen complicated scripts, thinking, this is ridiculous, it should be easy, I finally found your perfect form mailer.  Thanks." Y.J., May 15, 2006

"great script, exactly what I was looking for." S., March 13, 2006

"Thank you!  I've been trying to get my forms to work with my new server and your web page helped me tremendously.  How long I've searched the net for the answer.  I feel like I owe you one." L.W., February 20, 2006

"thank you" P., February 2, 2006

"a very good scipt" B.W., February 1, 2006

"Good Script" C., January 13, 2006

"yes u r really good" D.S.M., December 18, 2005

"Works Perfectly - thank you so much" M.Z., November 27, 2005

"Neato Thanks!" B., November 20, 2005

"Thats cool" C.M., November 9, 2005

"...This is a great script.  Thank you." D., October 26, 2005

"Sure enough, I tried it, and it works wonderfully!  I hate using the FormMail perl script!  Thanks!" D., September 1, 2005

"You totally rock!  That worked." C.L., August 2, 2005

"Wonderful, this makes everything much more simple.  Will get good use out of it" H.T., July 11, 2005

"nice script" Anonymous, July 2, 2005

"Hi there, This is it.  Looked everywhere.  This is simple AND Works!  Thanks" R.v.D, June 7, 2005

"PERFECT works great.  Thank you." I.U., June 7, 2005

"I spent days fooling with other scripts.  I installed them, only to find that they didn't work.  Then I found PERFECT, and it works, well, PERFECTly!" Z., May 6, 2005

"This is SO much easier than mucking about with CGI.  Thanks!" L.R., April 17, 2005

"Good teaching of these scripts." C., April 14, 2005

"awsome, it not only solve my problem, it also let me do whatever form customization that i want, thx" R.Y., April 5, 2005

"OMG!  I spent 3 hours browsing google looking for a script to do this.  Finally, I clicked on this link and ~BAM!~ it's JUST what I need.  You were more helpful than the company I bought my site from!  Thank you SOOOOO much!!!" B., April 5, 2005

"Nice one.  Just what I was looking for; short and sweet.  Big thanks..." R.J.H., March 27, 2005

"Awesome.  Best.  Short, Sweet, Fully packed.  Thanksamundo!" C.J., March 26, 2005

"this script is crazy smart.  Evil almost.  Thanks." R.S., February 23, 2005

"...I love your script!..." D.R., February 7, 2005

"I like your script.  It's very easy to use..." K.H., January 25, 2005

"Oh, this script is the best, for sure.  Simple, powerful, and easy to understand, just like PHP is.  Congratulation for your good work and cleverness." F., December 31, 2004

"...it works great.,,,,thanks for everything." N., September 20, 2004

"Hello!  Thank you for your quick and simple email form script!..." E., September 14, 2004

"Your form processor is FAB!  I take my hat off to you!" D., September 7, 2004

"Great and simple script as you said.  Just tried it and thank you..." M.V., September 2, 2004

"finally a short and effective script!  THX a lot for putting it out there." A., August 12, 2004

Comments

Send us a message

Powered by PERFECT

All the fields are optional.  However, if you want a response, make sure to provide your email address.

Special thanks to Greg for his technical advice in creating PERFECT.