PHP email form not working

I trying to add a PHP contact form to my web site where an user can send an email from the form and send it to a friend.

very basic at the moment just to get it to work but on my Apache server when I run the script I get the message.

Warning: mail(): SMTP server response: 503 No valid recipients specified. in C:\xampp\htdocs\date\testmeil.php on line 12

refer to this line of code: $ok = mail($to, $subject,$message, $headers );

The PHP script seem to me that it is running before I get a chance to fill out the form. There is a problem in the PHP script as the mercury mail service has not received a message from the php script.

I have a Test web domain which I up loaded to that thinking it might been just my email and php server having a hissy fit but still not luck soon as it loaded and had a chance to fill out the contact form I had a message as the top of the page say email sent ok and I done nothing other then load the page.

Can someone tell me where or what I am doing wrong.

[php]<?php

$to = $_POST['emailto'];
$from = $_POST['emailfrom'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];

$message = " this is a test and this is what you have entered" .$comments;

$headers ="from:".$from ;

	$ok = 	mail($to, $subject,$message, $headers );
if($ok) echo "sent fine";
	else echo" not sent fine";

?>[/php]

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<form action="sent.html" method="post">

TO <input type="text" name="emailto" value="yourname@yourdomain" /><br />
FROM<input type="text" name="emailfrom" /><br />
SUBJEC<input type="subject" name="subject" /><br />
MESSAGE <textarea name="comments"></textarea><br />
<input type="submit" name="submit" />



<body>
</body>
</html>

You are missing several brackets from your coding, and also you should include an “isset” function to ensure that spammers don’t get the url, and start spamming you or others. I’ve rewritten your code to reflect these changes. You can either use it as one document, or just pick them apart and replace your existing code.

I’ve tested this on several different servers (cpanel, iis, apache, and nginx).

If you are still seeing a problem, I don’t believe it would be the code, more likely an issue with your configuration. If it’s not true apache (which is anything other than an actual linux server running http daemon) then you might need to enable php mail(). Check your documentation to make sure you can use it with that type of server.

[php]

Untitled Document

TO

FROM

SUBJEC

MESSAGE


<?php if (isset($_POST['send']) && ($_POST['send'] == "true")) { $to = $_POST['emailto']; $from = $_POST['emailfrom']; $subject = $_POST['subject']; $comments = $_POST['comments'];
$message = " this is a test and this is what you have entered" .$comments;

$headers ="From:".$from ;

$ok = 	mail($to, $subject,$message, $headers );

if($ok) {
echo "sent fine";  	
} else { 	
echo" not sent fine";
} 

}
?>[/php]

Thank you bradjtrammell that help a lot

isset won’t stop spammers, only some sort of human verification can do that. All you have to do is hit f5, the browser will automatically remember the last POST sent to the server.

I never said it was going to stop spammers. It’s just a loophole that I’ve seen used before to hijack mail scripts.

Thanks for the input though :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service