Intro
Additional Header Info
An Example Free Script
Sending...
Here on C-W-M we have had a lot of interest lately in using PHP mail functions. PHP provides an easy way to collect information on your website. Everything from a contact form to an online survey, to a ?Tell a Friend? script can be built from a few easy to learn pieces of code. This brief tutorial is the first part of a series PHP Mail Form Script Tutorials.
This is mail function for PHP in its simplest form:
Code: mail ( string to, string subject, string message)
The arguments are strings. So that using it might look like:
Code: mail ( ?to@emailaddress.com?, ?This is my subject line?, ?This is my message. \n The \n to the left of this text makes a new line?)
You can send additional arguments to the function including additional headers and parameters.
ie:
Code: mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])
Reference the mail function in the PHP manual here:
http://www.php.net/manual/en...
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 .
Intro
Additional Header Info
An Example Free Script
Sending...
The most common additional header info I often add is who the email is from:
"From: me@myemailaddress.com "
Usually I add the info to variables first like this:
Code: $mailto = ?to@email.com?;
$subject = ?Subject line text?;
$message = ?This is my message text \n
This is another line\n
And this is another.?;
$header = "From: me@myemailaddress.com";
Then call the mail function:
Code: @mail($mailto, $subject, $message)
Notice that the @ symbol suppresses any errors. It is easy to do our own error checking, as the function returns true when successful and false when not.
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 .
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 .