PDA

View Full Version : PHP form to mail


iFerret
2009-10-14, 04:35
So I'm writing up a contact form in PHP and it just does not work. I've tried looking at exemplars online and going through it myself. I can't find anything glaringly obvious that's wrong with it though. I'm hoping that somebody here can help me figure out what the problem is.

Here is the php code, which lives in a file called "booking.php":

<?php
$to = $_REQUEST['emailgoeshere@domain.co.nz'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "COMPANY Booking";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Date"} = "Date";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"Time"} = "Time";
$fields{"Message"} = "Message";

$body = "The following was recieved from the Bookings form:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = "From: noreply@domain.co.nz";
$subject2 = "Thank you for contacting COMPANY";
$autoreply = "Thank you for contacting COMPANY. Mike will get back to you as soon as possible, usually within 48 hours. Please consult our website at www.domain.co.nz.";

if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://domain.co.nz/thankyou.html" );}
else
{print "We encountered an error sending your mail, please press back and try again."; }
}
}
?>

Here is the applicable HTML code from the page the form will be in:

<form method="post" action="booking.php">
<table bgcolor=#ffffcc align=center>
<tr><td><font color=red>*</font> Name:</td><td><input size=25 name="Name"></td></tr>
<tr><td><font color=red>*</font> Email:</td><td><input size=25 name="Email"></td></tr>
<tr><td>Date you would like to book (DD/MM/YY):</td><td><input size=25 name="Date"></td></tr>
<tr><td>Time you would like to book:</td><td><input size=25 name="Time"></td></tr>
<tr><td>Phone:</td><td><input size=25 name="Phone"></td></tr>
<tr><td colspan=2>Message:</td></tr>
<tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr>
<tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr>
<tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr>
</table>
</form>


Every single time I attempt to do a test run of the form, it fails sending me the failure message from the PHP - "We encountered an error sending your mail." BTW, the email addresses and domains have been changed at the request of my client.

Hoping you can help me to work out where I've gone wrong with this (or suggest a better way to accomplish the same thing). FWIW, this is my first dabbling with PHP aside from ready made stuff like a forum I used to mod for. I looked at doing it with CGI (which I also have no experience with), but decided PHP appeared to be the easier option that worked better. Thanks in advance.

ryanajarrett
2009-10-14, 05:17
Could it be this line:

$to = $_REQUEST['emailgoeshere@domain.co.nz'] ;

Why not replace it with:

$to = "emailgoeshere@domain.co.nz";

No need to request the to email address as it is hardcoded (unless you want to make it a hidden field)

Hope this helps,

Ryan

iFerret
2009-10-14, 07:02
That is exactly it. Thank you so much - it actually works now, right down to the last step. I hadn't actually realised that was what I was doing (well I did, but I got it confused with the array and it got all messed up in my head.Thank you thank you thank you.

Having it as a hidden field seems like an interesting, but pointless alternative. Why do that when you can just hardcode how you've recommended? That's irrelevant anyway, because my problem is solved. Speedy response/turnaround time too :D.

Thanks again :)

ryanajarrett
2009-10-15, 06:59
No problem. I just threw in the hidden field thing unless you wanted it to be requested for some reason (e.g. it makes the code reusable in case you wanted it to go somewhere other than emailgoeshere@domain.co.nz)