Intro
Additional Header Info
An Example Free Script
Sending...
By way of a mini-tutorial, below is a simple implementation of the PHP mail function in the PHP Mail and Contact Form Script available on C-W-M here. http://www.christian-web-mas...
Here is the script in its entirety:
Code: <?php
// ************Begin Configure***************
//Put where you want the email to go
$mailto = "your@email.com";
//Put your subject in here
$subject = "Your subject here.";
//Put where to redirect to after sending the email
$redirect = "thankyoupage.html";
// ************End Configure****************
foreach($HTTP_POST_VARS as $key => $value) {
$message .= $key . ': ' . $value;
$message .= "\n"; //Note the double quotes
}
if (@mail($mailto, $subject, $message)) {
header("Location: $redirect");
} else {
// This echo's the error message if the email did not send.
// You could change the text in between the <p> tags.
echo('<p>Mail could not be sent. Please use your back button to try again.</p>');
}
?>
Now, I?ll talk about each part.
First:
Code:
// ************Begin Configure***************
//Put where you want the email to go
$mailto = "your@email.com";
//Put your subject in here
$subject = "Your subject here.";
//Put where to redirect to after sending the email
$redirect = "thankyoupage.html";
// ************
This is simple enough. It is the configuration section of the script, where variables to be used in the script are assigned values. This script is meant mainly to be used as a contact form on your site. The $mailto variable, then becomes your email address and the $subject variable, becomes something like ?Contact from my site?.
The $redirect variable allows the webmaster to specify a page to redirect to if the mail was sent successfully. This is typically a ?Thank you for contacting us? page.
Next:
Code:
foreach($HTTP_POST_VARS as $key => $value) {
$message .= $key . ': ' . $value;
$message .= '\n';
}
The idea behind this script is that you can take any generic form with any number of inputs (text, check box, ect.) and submit to it. The script then takes each form variable posted to it and processes them into the $message variable.
This is accomplished in the foreach loop on the HTTP_POST_VARS associative array (the posted variables). By the way, the new and better way to access post variables is through $_POST, but this script is backwards compatible.
The $key is the variable name from the form variable (such as a text input) submitted to it, and the $value is the value of the form variable. So a text box called ?name? would contain the value that the person who submitted the form put into it. Thus in the foreach loop when the form variable ?name? is processed, the ?name? key and its associated value are added to the message. It looks like ?name: My Name?. Then a new line is added to the $message text for the next key and value.
Sean Buscay
www.christian-web-masters.comSean Buscay is on a mission to CREATE, ADVANCE, and EXPOUND faith in JESUS using web technology. He is the owner of Christian-Web-Masters.com .