Page 1 of 1

Help with PHP contact form!

Posted: Wed Jun 24, 2009 3:32 pm
by ctate76
All,

I am a PHP newbie but have tried everything humanly possible to get my PHP contact form to work and it still will not send an email. I would love some help as I'm trying to finish up a project.

Currently my PHP file resides in the same directory as the HTML file.

The HTML

Code: Select all

<form method="POST" action="contact.php">
                        <p>*All fields are required</p>
 
                        <p>Email: <br>
                        <input type="text" name="Email">
                        <p>Full name: <br>
                        <input type="text" name="Fullname">
                        <p>Address:<br>
                        <input type="text" name="Address1">
                        <p>City, State Zip:<br>
                        <input type="text" name="Address2">
                        <p>Telephone:<br>
                        <input type="text" name="Telephone">
                        <p>Best time for us to reach you:<br>
                        <input type="text" name="Time">
                        <p>Tel:<br>
                        <input type="text" name="Tel">
                        <p><input type="submit" name="submit" value="Submit">
                        </form>
 
The PHP

Code: Select all

<?php
 
$EmailFrom = Trim(stripslashes($_POST['Email'])); 
$EmailTo = "christian.tate@gmail.com";
$Subject = Trim(stripslashes($_POST['Fullname'])); 
$Name = Trim(stripslashes($_POST['Address1'])); 
$Address = Trim(stripslashes($_POST['Address2'])); 
$City = Trim(stripslashes($_POST['Telephone'])); 
$State = Trim(stripslashes($_POST['Time'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
 
// validation
$validationOK=true;
if (Trim($Email)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}
 
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Fullname;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address1;
$Body .= "\n";
$Body .= "City, State Zip: ";
$Body .= $Address2;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Best time to contact: ";
$Body .= $Time;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
 
// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
 
// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Re: Help with PHP contact form!

Posted: Wed Jun 24, 2009 4:11 pm
by Eric!
What errors are you getting? None? No errors and no email?

mail() often echos non-zero results even when the mail goes out ok.

can you echo out the
$EmailTo, $Subject, $Body, "From: <$EmailFrom>"
Fields for us?

I'm not sure your last field is correct, this is where you usually put the header information, but it might work. I also wouldn't just trust stripslashes to filter your fields. Here's a link to some code which stops a variety of injection attempts in a readable mannor without calling php functions. Take a look at the header construction and injection filters I used in _mail.php listed about 1/2 down on the posts in the link....viewtopic.php?f=1&t=101854

FYI -- also set your return path is very important. If the header comes through with formatting errors, like missing a return path, many spam filters will filter it out

Code: Select all

$to="you@your.domain";
$from="me@my.domain";
ini_set(sendmail_from,$from);  // the INI lines are to force the From Address to be used !  
$returnpath="-f ".$from;  // Forces the return path to be configured properly   
$mail_sent = mail($to, $subject, $content, $headers, $returnpath);

Re: Help with PHP contact form!

Posted: Wed Jun 24, 2009 4:57 pm
by ctate76
Hey, thanks for the response

I get a 404 error after it appears to try and send the email and I have the form sending to my personal email and I haven't seen anything come in.

I'm totally new when it comes to this stuff so what exactly do you mean when you ask to echo those fields?

Re: Help with PHP contact form!

Posted: Wed Jun 24, 2009 5:15 pm
by Eric!
put this line of code before your mail() call

Code: Select all

echo 'EmailTo='.$EmailTo.'<br>Subject='.$Subject.'<br>Body='.$Body.'<br>From=From: <'.$EmailFrom.'>';
replace line 49 with

Code: Select all

echo '<br>mail returned='.$success;
Then post the output back here.

Also please take the time to look through my code about email injection prevention found in the link viewtopic.php?f=1&t=101854 in the code _mail.php about 1/2 way down.