I'll tell you when you pay me money! Why don't you learn php yourself? Is this for school? etc etc...
Hmm, yeah I grabbed the code and had more errors than you! I've moved a bit of stuff around and managed to get it working. I'll post the whole page, hopefully you'll be able to pick out the bits you want.
Code:
<?php
session_start();
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
?>
I moved the session start up here as it was header erroring
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Mailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$result = 0;
// CONFIG
$mail_to = "[email protected]";
// END CONFIG
function strip_mail_headers_single( $string ) {
return preg_replace('/(%0A|%0D|\\n+|\\r+)/i', '', $string);
}
function strip_mail_headers_multi( $string ) {
return preg_replace('/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i', '', $string);
}
I grabbed a new preg_match off php.net as Rob's was throwing errors at me. If his works ok on yours then use that but this one looks ok
Code:
function is_valid_email( $string ) {
$regex = '/\A(?:[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+'
.'(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@'
.'(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|'
.'com|org|net|gov|biz|info|name|aero|biz|info|jobs|'
.'museum)\b)\Z/i';
return preg_match($regex, $string);
}
function send_mail($to,$from,$from_mail,$subject,$message) {
This next bit seemed to be throwing the error, but when I echoed out an error line it showed when there was one, but not when there wasn't so I figured it was the actual returning of the mail function that was causing the problem so I moved it.
Code:
if (empty($from) || empty($from_mail) || empty($subject) || empty($message))
{
return -1;
}
I know you ditched the time check, but I switched this to a greater than rather than less than as you want the time last mailed plus 3 minutes to be less than the time now to return the -2 error. I just did it plus 1 for testing.
Code:
//if($_SESSION["last_mailed"] + 180 > time())
if($_SESSION["last_mailed"] + 1 > time())
{
return -2;
}
if(!is_valid_email($from_mail))
{
return -3;
}
}
if ( !empty($_POST) ) {
Didn't need to pass POSTs to variables but thought it made it easier
Code:
$post_from = $_POST["from"];
$post_from_mail = $_POST["from_mail"];
$post_subject = $_POST["subject"];
$post_message = $_POST["message"];
$result = send_mail($mail_to,$post_from,$post_from_mail,$post_subject,$post_message);
if($result == -1)
{
echo "<p>Whoops! You need to complete all the fields.</p>";
}
elseif($result == -2)
{
echo "<p>Whoah, slow down there cowboy! You can only send one mail every three minutes.</p>";
}
elseif
(
$result == -3 )
{
echo "<p>Please enter a valid email address.</p>";
}
else
{
This is where I moved all the mail functions to
Code:
$from = strip_mail_headers_single($post_from);
$from_mail = strip_mail_headers_single($post_from_mail);
$subject = strip_mail_headers_single($post_subject);
$message = strip_mail_headers_multi($post_message);
$_SESSION["last_mailed"] = time();
Not really neccessary to do this $ok bit, just another failsafe
Code:
$ok = mail($mail_to, $subject, $message, "From: $from <$from_mail>\r\n");
if($ok)
{
echo "<p>Mail sent successfully!</p>";
$post_from = "";
$post_from_mail = "";
$post_subject = "";
$post_message = "";
}
else
{
echo "<p>Sorry, your mail could not be sent at this time</p>";
}
}
}
?>
I put in autofills on the values cos I got fed up retyping on test.
Code:
<form method="post" action="">
<p><label for="from">Your name:</label></p>
<p><input name="from" type="text" id="from" value="<?php if($post_from) { echo $post_from; } ?>" /></p>
<p><label for="from_mail">Your email address:</label></p>
<p><input type="text" name="from_mail" id="from_mail" value="<?php if($post_from_mail) { echo $post_from_mail; } ?>" /></p>
<p><label for="subject">Subject:</label></p>
<p><input type="text" name="subject" id="subject" value="<?php if($post_subject) { echo $post_subject; } ?>" /></p>
<p><label for="message">Message:</label></p>
<p><textarea name="message" id="message"><?php if($post_message) { echo $post_message; } ?></textarea></p>
<p><input type="submit" value="Send Message" /></p>
</form>
</body>
</html>
Hope that works ok.