Some PHP Help

NOTE: There have been exploits to php mail scripts. You should secure your code.
To learn how, click the link below:
The php mail forms generated on this site contain code to stop exploits.

Below you'll find some php mail examples.  You can choose a link from the php main page to make a mail-sending script.
    Here's a simple form that sends a canned message. The body of the message is between the purple quotes. Email messages are sent in the order:
    NOTE that with global variables turned off, you need to declare these variable names and their values with something like what follows. The names are the form field names and they are CaSe SensiTiVe! You want to put something like this before any of the other script that deals with the form post. The very top of the page is a good bet.
if (!empty($_POST)) {  << Sets up the 'if' condition. Basically, if there is a form post to this file that isn't empty...
$_POST['Name'];  <<< There MUST be a form field on the form named Name.
$Email=$_POST['Email'];  <<< There MUST be a form field on the form named Email.
$Message=$_POST['Message'];}  <<< There MUST be a form field on the form named Message.
Ad nauseum. You get the picture. Continue by following the example, with the rest of your form posts.
End this if statement with a closing bracket once you have all the forms in. I put it after the 'Message' variable line and colored and bolded it so that there is no mistake. the brackets surround an if function.

     Now that you have your variables extracted from the form post with the if function above:
Below, "TO" is a variable called $to. You can put your email in the variable, or have it look it up from somewhere else. The fields in the form are just name and email. The "$Name" refers to "Name" field, and the "$Email" refers to the "Email" field, etc...because you determined that in your if statement above .     I matched the colors for clarity. In the BODY of the email message, line breaks are separated by "\n". Since mail servers identify senders as "Name: <>", the Name part can be anything. I would keep the <email address> portion local to your server (wherever the page is) to lower the chance of mail that looks relayed. That's the same reason I suggest plugging the person's name (from the Name field) in the FROM part of the email, instead of their email address.

    This piece of code contains "if and else. The if is "if the Email field is not blank". The else, is anything else, like the person arriving to the page. So, below else, you can put what you want the user to see upon arrival to the page, before submitting the form. The if covers what to do if the form is submitted. Notice that below the mail sending portion, it writes a thank you onto the page, saying to send another. You can have either script send a message to the person by doing the following:
Below the line that says:
mail($to,"Subject of the Email Here","From Web Page.....
Put in:
mail($Email,"Subject","Dear , $Name \nThanks for visiting our website. We appreciate your comments.\nSincerely, Your Signature here.","From: $Email");
Change the body to whatever you want to say.
You can include any $Field Names.
NOTE: Whenever you put special characters (such as quotes) inside a php print or echo statement, but a backslash (\) in front of it. Avoid using an apostrophe in an email body sent in this fashion.
This script is good for "send to a friend" or something like that.

$to = "";
$from_header = "From: $Name <$Email>";
if($Email != "") {
mail($to,"Subject","Mail from Web Page...\n$Message\nSincerely,$Name\n$Emaill ",$from_header);
print ("<font face=Arial color=066CC>Thanks! Send another if you like!</font>");
else {
echo "<b><u>Send me a note!</u></b><br>";
<form method="POST" action="<? echo ($PHP_SELF); ?>">
Your name: <input type="text" name="Name" size="30">
Your email: <input type="text" name="Email" maxlength="255" size="40">

Your Message: <textarea name="Message"></textarea> <<< Note this is a multi-line field.
<input type="submit" name="Submit" value="Send it">

The script below is almost identical to the one above, but it sends name, address, email and comments field as the body of the message, and uses a different method of how it handles the "if - else" conditions. Also, the subject of the email will say "Mail from ('the location of the page). Click Here to see an example of this exact functional code which will send me an email.

$to = "";
$from_header = "From: $Email"; ?>
<?php if($Submit): ?>
mail($to,"Mail from $PHP_SELF",Name: "$Name\nAddress: $Address\nEMail: $Email\nComments:\n$Comments",$from_header);
print ("<font face=Arial color=066CC>Thank you for your comments.<br>We will get back to you soon.</font>");
print ("
This is what shows after submission. The if(\$Submit) statement sets this text.");
<? php else: ?><hr>Anything below this,(the else statement) and above the "endif" is what will be done (printed on the page) if the form hasn't yet been submitted:

<p><font face="Arial"><strong>Please submit your information below:</strong>
form method="POST" action="<? echo $PHP_SELF; ?>">
Your name: <input type="text" name="Name" size="30">
<br>Your address: <input type="text" name="Address" size="30">
<br>Your email: <input type="text" name="Email" size="30">
<br>Your comments:<br><textarea name="Comments"></textarea>
<input type="Submit" name="Submit" value="Send it">

<? php endif; ?> <hr>Anything here (after the endif) goes on the page no matter what.

Press H for TWebMan Home |J for JavaScript | P for Make A Perl Script | S for Make a PHP Script