Code:
$EmailFrom = trim($_POST["EmailFrom"]);
$EmailTo = "owen.timothy@gmail.com";
$Name = trim($_POST["Name"]);
$QuestionorComment = trim($_POST["QuestionorComment"]);
mail($EmailTo, "Email from website", $QuestionorComment) {
if(!preg_match("@.");
return false;
elseif(return false);
else;
}
Okay that whole preg_match thing as far as i know completely wrong..
You should do ALL checking and processing of fields BEFORE the mail() function
then if theres no errors in the formatting and hat and all your checks validate, then run mail()
else show error.html
something like this should do what you need:
PHP Code:
<? function validate_email($email) { $email = trim($email); if( (strpos($email, "@") === FALSE) || (strpos($email, ".") === FALSE) || (strpos($email, " ") != FALSE) || (strpos($email, "@") > strrpos($email, "."))) { $valid_email = 0; } else { $valid_email = $email; // If email IS valid the email becomes $valid_email if it isnt it become 0 (false) } } function clean_input($input) { if(empty($input)) { $input_error++; //will add 1 to $_input_error later on we will check if $_input_error is > 0 if so dont send } else { $input = trim($input); $input = htmlentities($input); // Removes any HTML tags etc within the input } } $to = 'owen.timothy@gmail.com'; $from = 'owen.timothy@gmail.com'; // I would advise you chnage this to a email one your server messages will be sent from this address $email = validate_email($_POST['email']); $Name = clean_input($_POST['Name']); $message = clean_input($_POST['QuestionorComment']); //You should really think about using short names like message. :D $subject = $Name.' Has contacted you via your online form.'; // this is the subject of the email # The following makes the BODY of your email. $body = $Name.' Sent you a email!'; $body.= '<br /> The message they left: <br />'; $body.= $message; $body.= '<br />'. $Name.'\'s Email is: '.$email.'<br />'; $body.= 'This email was generated automatically via a PHP emailer created by <a href="http://dansgalaxy.co.uk">Dansgalaxy</a> :D'; $body.= '<br /><br /><br />'; // Feel Free to remove the above line :) all it will do is send "This email was automatically via a PHP emailer created by Dansgalaxy" #Now we are constructing out headers for the email $nl = "\r\n" //new line and return $headers = ''; $headers.= "From: ".$Name. "<".$from.">" .$nl; $headers.= "Reply-To: "$Name. "<".$email.">" .$nl; //this should should me when you reply to a message it will send it to the users and NOT your email which is was sent from. ### This should try amd help the emails not get sent to junk. ### $headers.= "Message-ID: <".time(). "-" .$from.">".$nl; $headers.= "X-Mailer: PHP v".phpversion(). $nl; $headers.= "Content-Type: text/html; charset=iso-8859-1".$nl; if($valid_email == 0 OR $input_error > 0) //if email returned false for validity or there was a input error dont send show error message. { echo 'ERROR!! Email <strong>NOT</strong> sent!'; } else { mail($to, $subject, $body, $headers); echo 'Email successfully Sent!'; } ?>
ok i havent tested this script but i just knocked it up and i have to say im quite pleased with it
this is much more than the basic, and u can easily edit it and add extre fields and stuff which is why i made the clean_input and validate_email functions os you can use them again and again.
please test this and if theres any problems feel free to contact me!
Talkupation will be MUCH apprieciated... as just got ear full for being one PC at 2PM :P
Thanks,
Dan 
Last edited by dansgalaxy; 08-28-2007 at 09:56 PM..
Reason: Fixed Error
|