ahh new post will do:
just have the function abailable either in the page or included, then call it with the arguments somewhere on the page, no need to check for if the form has been posted. It'll return false if it doesn't send an email, true if it (thinks it) did successfully.
you could pull it out of the function and streamline it into teh code, just predefining the to and from variables, that'd allow you to easily add messages for missing required values, or failure reasons, etc.
<?php
/* free script anyone can use edit alter sell give away or put in a box full of spiders and video it for youtube. I really don't care */
/* untested, also :) */
// call this with the email address you want to send to, the email address you want it to come from (usually "noreply@domain.com) and if you want to require all post vars have values (very bad way to verify, but also very simple)
function mailForm ( $to, $from, $require = false )
{
// first check we have a valid to and from email address - don't let a user submit these, or you'll be ripe to become a spammer by proxy.
// regular expression for emails, change it if you like, it should cover most valid email addresses
$email_reg = "^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$";
if ( !eregi( $email_reg, $to ) )
{
die( 'Invalid TO email address' );
}
else if ( !eregi( $email_reg, $from ) )
{
die( 'Invalid FROM email address' );
}
if ( is_array( $_POST ) && count( $_POST ) // check we have posted information
{
// initialise our variable to contain teh data
$data = '';
foreach ( $_POST as $field => $value )
{
// remove any leading or trailing whitespace.
$value = trim( $value );
if ( $require && empty( $value ) ) // if values are required and this one is empty
{
// could return or collate error message(s) here, but we'll just return false for this simple script
return false;
}
// spit out the field name, replace underscores with spaces, capitalize the words, add a colon and a space.
$data .= ucwords( str_replace( '_', ' ', $key ) ) ).': ';
// add on the value they posted
$data .= $value;
$data .= "\r\n"; // add a newline - overkill style
}
$body = "\r\n\r\n"; // prevent header injection from the (well as we're starting this email ourselves, this won't happen, but if you edit teh body, leave this here)
// add on a date (easily human readable, of course!)
$body .= "Form Submitted on ".date( 'g:ia l, js F, Y' )."\r\n";
// add on the data
$body .= "Submitted data:\r\n".$data;
// we have our posted data now, lets send out the email, and return it's success
// the "Form Submitted" subject could be a function variable, or you can edit it...
return mail( $to, "Form Submitted", $body, 'From: '.$from, "-f$from" );
// note, there were no expanded headers, it's all default, will be a plain text, not html format, but you could modify the 4th argument to contain all the mail headers you want.
}
// else isn't really needed if we havent returned earlier, we've got an else type case and will return as such
return false;
}
?>