Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

PHP Form Mailer

by Not2Sure   | filed under Web, HTML, CSS, and XML

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

We have all seen them and most people use them but PHP form mailers are become a must for a web site and I will show how to make one with no knowledge of PHP needed.

The PHP Code
Do not edit this unless you know what you are doing:

                    <?PHP

$date = date ("l, F jS, Y");
$time = date ("h:i A");

$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
  foreach ($_POST as $key => $value) {
  $msg .= ucfirst ($key) ." : ". $value . "\n";
  }
}
else {
  foreach ($_GET as $key => $value) {
  $msg .= ucfirst ($key) ." : ". $value . "\n";
  }
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {
  header ("Location:$location");
}
else {
  echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}

?>

[ save this as mailer.php ]

 Note
You can change the line:

"Thank you for submitting our form. We will get back to you as soon as possible."

With your own message. This is the message you will see after you submit the form.



Setting Your Properties
Change these settings to your information.


$to = "[email protected]";
Change "[email protected]" to your email. (you must keep the "" marks)

$subject = "Results from your Request Info form";
This will be the subject of the Email when it is sent.

$headers = "From: My Site";
This will be who it is from, usually My site or something like that.

$forward = 0;
redirect? 1=yes 0=no (this is the page you go to after you submit the form. if left on 0 you will see a message then nothing will happen if you select 1 you will see a message then go to the redirect page.)

$location = "yoursite.com";
This will be the page you are redirected. (if you keep it 0 you do not need one just delete "yoursite.com"; and leave ""; instead)


Now you have changed each option to your needs paste each line under the first line of the PHP code you made earlier (mailer.php)
 


 
[ copy each line like this but with you settings ]

HTML code

this is were the script becomes very useful. you can add as many input boxes, radio buttons, check boxes etc as you like with no change to the PHP code.

Make a new form using these settings.



And Between the >< add you input options
Help with Input options:

Single line text:


Text Area:


Submit Button:

When you add an input field add NAME="". Between the "" add a unique name for each field. So when you receive an Email it will look like this.
 


There are many more, if you get stuck ask on the forums.


That is it. make sure you PHP code is in the same directory as the HTML code or you can link to it like by changing action="mailer.php" to something like action="http://www.mysite.com/mailer.php" in the HTML code.

I hope this information helped. If you have any questions or comments, please don't hesitate to post them on the forums found by clicking here.

Regards,

Not2Sure

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going! 😇

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--