views:

103

answers:

6

I am trying to send an email message with data collected from an html form.

Here is the form:

<form action="estimate.php" action="post">
    <fieldset>
        <input type="text" name="name" value="FULL NAME" onfocus="if (this.value=='FULL NAME') this.value='';"/>
        <input type="text" name="phone" value="PHONE NUMBER" onfocus="if (this.value=='PHONE NUMBER') this.value='';"/>
        <input type="text" name="email" value="EMAIL" onfocus="if (this.value=='EMAIL') this.value='';"/>
        <input type="text" name="date" value="MOVE DATE" onfocus="if (this.value=='MOVE DATE') this.value='';"/>
        <input type="text" name="origin" value="ORIGINATING ADDRESS" onfocus="if (this.value=='ORIGINATING ADDRESS') this.value='';"/>
        <input type="text" name="destination" value="DESTINATION ADDRESS" onfocus="if (this.value=='DESTINATION ADDRESS') this.value='';"/>
        <select name="move-type">
            <option value="" selected="selected">TYPE OF MOVE</option>
            <option value="Private">Private</option>
            <option value="Commercial">Commercial</option>
        </select>
        <input id="quoteSubmit" type="image" src="_images/btn_submit.png" alt="" onmouseover="javascript:this.src='_images/btn_submit-over.png'" onmouseout="javascript:this.src='_images/btn_submit.png'"/>
    </fieldset>
</form>

Here is the PHP (which I got some help on in an earlier question posting):

<html>
<head>
<title>Contact Us</title>
</head>
<body>
<h2>Email Confirmation Page</h2>
<p>This is to prove that something is showing up.</p>
<?php # sends contents of  Free Estimate form

if (isset($_POST['submitted'])) {
$errors = array();

// Check for empty fields

$checkArray = array('name', 'email', 'date', 'origin', 'destination');
foreach($checkArray as $check) {
    if (empty($_POST[$check])) {
        $errors[] = 'Please enter your '.$check;
    } 
}

if (empty($errors)) { // everything is okay

$to = "[email protected]";

    $body = "The following Free Estimate Request has been submitted:\n

        Submitted by: {$_POST['name']}\r
        E-mail: {$_POST['email']}\r
        Phone: {$_POST['phone']}\r
        Move date {$_POST['date']}\r
        Moving from: {$_POST['origin']}\r
        Moving to: {$_POST['destination']}\r
        Move type: {$_POST['move-type']}\r;

    ";

    mail ($to, 'Free Estimate Request', $body, 'From:  '.$from);      

    // end email

} else {    
    echo '<h2>Error!</h2>
    <p class="errors">The following error(s) occurred:<br />';
    foreach ($errors as $msg) {
        echo " - $msg<br />\n";
    }
    echo '</p><p>Please go back and try again.</p><p><br /></p>';
}
};

?>
</body>
</html>

The estimate.php page shows up fine and no errors are generated, but I don't get any emails.

Not sure what I am missing here.

Thanks.

A: 

Did you check the return value of mail() to see if it succeeded? Also, an SMTP server needs to be running on the same machine as the web server for this call to succeed.

casablanca
A: 

The machine where you are running this, has it been configured to send email? The code seems fine, so maybe its a configuration problem, check this out.

Francisco Soto
A: 

Are you on a UNIX system? Check your system logs. PHP sends email by handing it off to the system mail service. You need to be sure that's working before PHP can send email.

Kalium
+2  A: 

Many things can do this and one of them is malformed email messages. This is quite easy to achieve with the PHP mail function if you don't know the RFCs/standards. Even if mail() returns true (success) it is possible that the email never reach it's destination.

The more (unobvious) causes of such failure are often:

Extra headers (like Reply-To) must be separated by \r\n:

Multiple extra headers should be separated with a CRLF (\r\n).

Lines in the the body must be separated by \n:

Each line should be separated with a LF (\n). Lines should not be larger than 70 characters

Currently you use \r and \n in the body which is incorrect (in some case it might work but it depends of many things out of your hands).

Other causes can be the lack of Reply-To header or even badly configured MX records which make all your email sent with PHP cought as spam.

The best (and easiest) solution IMHO is to stop using PHP mail function and switch to a library that will make a correctly formatted mail message. This way you don't have to learn all the tricky RFCs and you code is more secure if you use a good library (read about email injection attacks).

The one I use are Swift Mailer and PHPMailer.

AlexV
A: 

Are you running from a local server?You need to have a DNS setup in order to sent an email...

rabidmachine9
Thats not true, you only need a valit SMTP in the PHP.INI.
AlexV
Oh thanks for enlightening me...
rabidmachine9
+1  A: 

Try sending with a simple body:

$body = "The following Free Estimate Request has been submitted:\n";

See if it sends then.

also try a basic email:

mail ($to, 'Free Estimate Request', 'This is my body', 'From:  '.$from);

Also try changing different email address and check your junk folders.

And ofcourse make sure there is an SMTP server running

UPDATE:

You may also want to:

echo "<pre>";
print_r($_POST);
echo "</pre>";

Add this before your isset($_POST['submitted']); Then you can check to see if it even getting that far, and you can check your post vars

Lizard
Hi Lizard, I tried both of these and not luck. I am running this of Media Temple and don't think anything is missing off the server end.
fmz
Put an echo "test" before the first mail() call to triple check that it recognises that it is a submitted form. e.g. if (isset($_POST['submitted'])) { echo "testing"; }
Lizard
Also check my update
Lizard