PHP email contact form

Associate
Joined
7 Dec 2003
Posts
1,600
I'm trying to add a contact form to a site, and decded to use Rob's php form (http://robm.me.uk/articles/sending-mail-with-php)

I put the form on the contact page, and the rest in sender.php, and set the action of the form to post to sender.php. I removed the bit that checks how long since you last sent a message, because it didn't seem to work, and then it all worked, and sent the email.

But, even if all fields are filled in and the mail is sent and receivedin my inbox, it still presents the error message about all fields not being completed.

I copied the code exactly from robs site but it just won't work! What am I doing wrong??
 
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.
 
paulsheffII said:
Aha yeah but I've found some php setups that error if you try and echo a value that's not been set. Usually php plugin on Windows boxes, but you never know. ;)

i guess that depends on the level of error reporting set in php.ini? nowt to do with windows - i run php on windows and echo blank values on a form no problem... :p

if php.ini is set to display errors on warnings then that is just wrong for a production server. certainly wouldn't expect anywebhost to have that option enabled.
 
marc2003 said:
i guess that depends on the level of error reporting set in php.ini? nowt to do with windows - i run php on windows and echo blank values on a form no problem... :p

if php.ini is set to display errors on warnings then that is just wrong for a production server. certainly wouldn't expect anywebhost to have that option enabled.

I always employ the 'expect the unexpected' paradigm, and never assume that things like 'display errors' are turned off. For this kind of thing I would simply echo the values out, as you say, but I would use the suppress errors modifier to ensure that if it does fail, it fails quietly regardless of whether display errors is on or not :p
 
elkdanger said:
I always employ the 'expect the unexpected' paradigm, and never assume that things like 'display errors' are turned off.

i have display errors set to on and still don't get errors displayed. that would only happen if you set the error_reporting to show notices. it would be shocking for any server to have notices enabled? :p

but i guess you're right about 'expect the unexpected'.... :)
 
Back
Top Bottom