There is also a function that sends e-mail from PHP. You can take form results and compile them into an e-mail message, and send that e-mail to yourself.
The ‘mail’ function has three parts: the address you’re sending to, the subject of the message, and the body of the message. Add the bold sections below:
<?
$color = $_REQUEST["color"];
$name = $_REQUEST["name"];
IF (isset($color)):
IF ($color == "" || $name == ""):
echo "<p>You need to enter a color and a name!</p>\n";
ELSE:
//send me an e-mail with their favorite
$subject = "$name's favorite color";
$sendto = "youraddress@wherever.com";
$message = "$name said their favorite color was $color.";
mail($sendto,$subject,$message);
//display their favorite color
$color = strtolower($color);
$color = trim($color);
IF ($color == "white"):
$bgcolor = "grey";
ELSE:
$bgcolor = "white";
ENDIF;
?>
<p style="color: <?echo $color?>; background-color: <?echo $bgcolor?>;">
You said your favorite color was <?echo $color?>.
</p>
<?
ENDIF;
ELSE:
echo "<p>Welcome to our color extravaganza!</p>\n";
ENDIF;
?>
<form method="post" action="test.php">
<p>
What is your favorite color?
<input type="text" name="color" value="<?echo $color?>" /><br />
What is your name?
<input type="text" name="name" value="<?echo $name?>" /> </p>
<input type="submit" />
</form>
Here, the function (mail) takes more than one “argument”. Each item between commas, between the parentheses, is an “argument” to the function. We’re sending “mail()”arguments to specify the address the message should go to, the subject of the message, and the body of the message.
Don’t forget to replace “youraddress” with your e-mail address!
Finally, notice the two lines that begin with double slashes. PHP ignores any line that begins with double slashes. We can use this to put comments in our script. When you have any script larger than a few lines, it is very useful to comment each piece of the script so that you can remember what that piece’s purpose is later.
Never send an e-mail to an address collected on an unprotected form. Remember the color lesson: you can’t trust that the person on the other end is honest--or even that they’re a person. Don’t let your forms become spam sources.