Sending a HTML form with a PHP script

Soldato
Joined
11 Apr 2004
Posts
19,914
This has been bugging me for ages. I've tried several different bits of code yet none seem to have worked :(

Basically, I've got a HTML form that I want to send to an e-mail using a PHP script.

The HTML form has the the code...

Code:
<form name="booking" method="post" action="sendform.php">

but I'm having trouble finding a PHP script that actually works. I've tried a few examples but they don't seem to work.

I'd really appreciate it if someone could help me out with this, as I've been trying to do it for weeks and can't get it to work. I know my webserver is working and the correct SMTP server is set up in the php.ini file but for some reason I'm not receiving any e-mail from the script.

Thanks!
 
Sending mail is pretty simple and easy to debug if everything's configured correctly. Might be worth making a simple page using php's mail() function and testing that everything's working correctly. Once that's done, post the php code you're working with for us to have a look at. Can't tell what's wrong with it if we can't see it!! :)
 
Hi Sic, thanks for the reply :)

I'll give it a shot but I was just hoping that someone could give me a sample of code which I could then go away and edit to suit my requirements.

I've goto a book somewhere (one of those Teach yourself PHP in 24 hours) that I might dig out and have a read of.
 
I'll be more than glad to help you, but i'm currently posting on my phone, so writing php will probaby end in violent suicide right now! I'll write you a small sample of code tomorrow morning, and hopefully that'll help you.

I'd still recommend creating a php file with just the following in:
Code:
<?
mail('[email protected]','test','this is a test email, everything is ok');
?>
Obviously change the email address to yours!
 
haha, I completely understand.

Cheers for that code, I've tried it but don't seem to be receiving anything. I know my mail server is working fine as I can send/receive manually.

My php.ini file contains this...

Code:
[mail function]
; For Win32 only.
SMTP = mail.r8technology.co.uk
smtp_port = xxx

; For Win32 only.
sendmail_from = [email protected]

Should there be speechmarks around the server and port info?
 
Hi,

I don't think it's the code that's the problem as I copied/pasted a sample HTML form and PHP script from a tutorial page to test, and that didn't seem to work either.

Also, when trying a direct e-mail from PHP, I'm not recieving anything.

Once I've started receiving e-mails, I should be ok with the actual code as I'm now aware that it's a config problem.

edit... This is the sample code I used last night when my original page didn't seem to be working...

html
Code:
<form name="form" method="post"
  action="contact_thanks.php">
<p class="bodymd">Your Name<br>

<input type="text" name="Name">
</p>
<p class="bodymd">Your Email<br>
<input type="text" name="Email">
</p>
<p class="bodymd">Comments or Questions<br>
<textarea name="Comments" rows="5" cols="40">
</textarea>

</p>
<p class="bodymd">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Form">
</p>
</form>

PHP
Code:
<?php

if (($Name == "") || ($Email == "") || ($Comments == ""))
{
	echo "<form name=form method=post action=contact_thanks.php>";
	echo "<p class=bodymd>All three fields of this form are required.</p>
	echo "<p class=bodymd>I really don't think that's too much to ask...</p>";
	echo "<p class=bodymd>Fill in the ones you missed, they are listed below.</p>";
}
if ($Name == "")
{
	echo "<p class=bodymd>Your Name<br><input type=text name=Name></p>";
}
else
{
	echo "<input type=hidden name=Name value=$Name>";
}
if ($Email == "")
{
	echo "<p class=bodymd>Your Email<br><input type=text name=Email></p>";
}
else
{
	echo "<input type=hidden name=Email value=$Email>";
}
if ($Comments == "")
{
	echo "<p class=bodymd>Comments or Questions<br>
   <textarea name=Comments rows=5 cols=40></textarea></p>";
}
else
{
	echo "<input type=hidden name=Comments value=$Comments>";
}

if (($Name == "") || ($Email == "") || ($Comments == ""))
{
	echo "<input type=submit name=Submit value=Submit>";
	echo "<input type=reset name=Reset value=Clear Form>";
	echo "</form>";
}
else
{
	$message = "Name: $Name\nEmail: $Email\nComments: $Comments\n";
	$extra = "From: $Name\r\nReply-To: $Email\r\n";
	mail ("[email protected]", "Website Email", $message, $extra);
	echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>";
	echo "<p class=bodymd>A response will be sent to $Email as soon as possible.</p>";
}
?>

I know most of that PHP isn't needed. Like I said, I just copied/pasted to see if it would work.
 
Last edited:
Fair enough - its just i don't know how new you are to PHP, so you could be doing some noob thing.

Try this:

Code:
<?

mail('[email protected]','test','this is a test email, everything is ok');
echo "done";

?>

....to see if you get the echo "done" working properly.


EDIT:

Also try the speech marks thing - I think there shout be speech marks around the server, but not the port.
 
Last edited:
Did you get the word "done" shown on screen?

----

Also try the speech marks thing - I think there shoud be speech marks around the server, but not the port.
 
Nope, I didn't.

Hmmm.

What could be causing that? I know PHP works because I have phpbb running and can call the phpinfo script.
 
No PHP expert myself - but I think it just means that there was an error with the previous line of code - supprised the error doesn't show though.

Try this:

Code:
<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE);

mail('[email protected]', 'My Subject', 'My Message');

echo "Done";

?>

Try a different e-mail address this time - although I doubt that is the problem.

Did you change the server to have quotes around it like this:

Please Note: Make a BACKUP!

Code:
[mail function]
; For Win32 only.
SMTP ='mail.r8technology.co.uk'
smtp_port = xxx

; For Win32 only.
sendmail_from = [email protected]
 
Ok, this time I got the 'Done' text.

I put "speechmarks" around the mail server, lol. Tried again with single quote marks now. Do you think that was it?

Haven't actually received the e-mail yet though... might take a few minutes.

Thanks very much for the help.

edit... The e-mails are sitting in the message queue on my e-mail server, so it did send them.
 
Last edited:
I think it was the destination e-mail address, as the two emails to external addresses are in the queue, whereas the email to andrew@r8 isn't... and certainly hasn't been delivered.

That's weird.
 
Right, been playing about with this all day and have found out that the following code seems to work everytime (even to the andrew@r8 destination)

Code:
<?php

$to="[email protected]";
$from="[email protected]";
$subject="hi";
$message="just a test message";
mail($to, $subject, $message, "$from\r\nX-Priority: 1 (Highest)" ) or print "Could not send mail";
?>

Whereas if you remove the $from section, it seems a bit hit and miss.

Is this normal? Or a configuration problem?

Oh, I also realised that I don't have short commands enabled, so therefore <? on its own won't work and you need <?php at the start of the code.
 
Yep. Definintely nothing there as that's a problem I had when I first set up this domain.

All e-mails coming from andrew@r8 and going to my Yahoo accouts were being sent to the junk box.
 
Back
Top Bottom