tags:

views:

230

answers:

6

The following code sends me an email holding the following variables. One of the last if statements says if(mail) then echo "You will be contacted soon". When the script runs I get echoed back "You will be contacted soon", however, I never receive an email.

I do have a smaller contact script (posted after this first and larger one) that does work.

Note: contants.php and functions.php are both included and work fine WEBMASTER_EMAIL is defined in contanstants.php and is correct, because my smaller contact script uses the same variable, and emails me fine.

Thanks for the help

 <?php

// pull constant variables
include("php/constants.php");

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post) {
    include ("php/functions.php");
}

// general info
$name = stripslashes($_POST['contact']);
$phone = $_POST['phone'];
$email = trim($_POST['email']);
$time_to_reach = $_POST['time-to-reach']; // what the best time to reach them?

// delivery info
$delivery_address = $_POST['del-address'];
$delivery_city = $_POST['del-city'];
$delivery_state = $_POST['del-state'];
$delivery_zip = $_POST['del-zip'];

// moving city info if applicable
$moving_address = $_POST['move-address'];
$moving_city = $_POST['move-city'];
$moving_state = $_POST['move-state'];
$moving_zip = $_POST['move-zip'];

// date needed
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];

// how long do you need the storage?
$storage_length = $_POST['time-length'];

// how many containers do you need?
$quantity_containers =  $_POST['number-of-containers'];

// how did you hear about us?
$tracker =  $_POST['tracker'];

// message
$message_holder = htmlspecialchars($_POST['message']);

$error = '';

// check general info
if(!$name) { $error .= 'Please enter your name.<br />'; }
if(!$email) { $error .= 'Please enter an e-mail address.<br />'; }
if($email && !ValidateEmail($email)) { $error .= 'Please enter a valid e-mail address.<br />'; }
if(!$time_to_reach) { $error .= 'Please select the best time to reach you.<br />'; }

// check delivery info
if(!$delivery_address) { $error .= 'Please enter you current address.<br />'; }
if(!$delivery_city) { $error .= 'Please enter your current city.<br />'; }
if(!$delivery_state) { $error .= 'Please enter your current state.<br />'; }
if(!$delivery_zip) { $error .= 'Please enter your current zip code.<br />'; }

// check date needed
if(!$month) { $error .= 'Please enter the approximate date you need the storage.<br />'; }
if(!$day) { $error .= 'Please enter the approximate date you need the storage.<br />'; }
if(!$year) { $error .= 'Please enter the approximate date you need the storage.<br />'; }

// check length of time needed
if(!$storage_length) { $error .= 'Approximatly how long will you need the storage unit for?<br />'; }

// check quantity of storages
if(!$quantity_containers) { $error .= 'How many containers will you need?<br />'; }

// check advertising tracker
if(!$tracker) { $error .= 'Please let us know how you\'ve heard of us.<br />'; }

// check message (length)
if(!$message_holder || strlen($message_holder) < 10) {
    $error .= "Please enter your message. It should have at least 10 characters.<br />";
}

// build email message
$message = "Name: {$name}
Phone: {$phone}
Email: {$email}
Best time to reach: {$time_to_reach}\n
-----------------------------------------------------
Delivery address: {$delivery_address}
              {$delivery_city}, {$delivery_state} {$delivery_zip}

Moving address: {$moving_address}
              {$moving_city}, {$moving_state} {$moving_zip}
-----------------------------------------------------
Date needed: {$month}/{$day}/{$year}
Length of time needed: {$storage_length}
Number of containers: {$quantity_containers}
Where did you hear about us? 
{$tracker}\n
Message: {$message_holder}\n";

if(!$error) {
    $mail = mail(WEBMASTER_EMAIL, $subject, $message,
         "From: [email protected]\r\n"
        ."Reply-To: ".$name."<".$email.">\r\n"
        ."X-Mailer: PHP/" . phpversion());

    if($mail) {
        echo '<p>Thank you, you will be contacted soon.</p>';
    }   
} else {
    echo '<div class="notification_error">'.$error.'</div>';
}

?>

The following script, contact script, does work meaning I receive an email.

<?php

// pull constant variables
include("php/constants.php");

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post) {
    include ("php/functions.php");
}

// variables
$name = stripslashes($_POST['name']);
$phone = $_POST['phone'];
$email = trim($_POST['email']);
$tracker = $_POST['tracker'];
$message_holder = htmlspecialchars($_POST['message']);

$error = '';

// check name
if(!$name) {
    $error .= 'Please enter your name.<br />';
}

// check email
if(!$email) {
    $error .= 'Please enter an e-mail address.<br />';
}

// validate email
if($email && !ValidateEmail($email)) {
    $error .= 'Please enter a valid e-mail address.<br />';
}

// check advertising tracker
if(!$tracker) {
    $error .= 'Please let us know how you\'ve heard of us.';
}

// check message (length)
if(!$message_holder || strlen($message_holder) < 10) {
    $error .= "Please enter your message. It should have at least 10 characters.<br />";
}

// build email message
$message = "Name: {$name} \n
Phone: {$phone} \n
Email: {$email} \n
Where did you hear about us? 
{$tracker}\n\n
Message: {$message_holder}\n";

if(!$error) {
    $mail = mail(WEBMASTER_EMAIL, $subject, $message,
         "From: [email protected]\r\n"
        ."Reply-To: ".$name."<".$email.">\r\n"
        ."X-Mailer: PHP/" . phpversion());

    if($mail) {
        //header("Location: thank_you.php");
        echo "Thank you. You will be contacted soon.";
    }   
} else {
    echo '<div class="notification_error">'.$error.'</div>';
}

?>
+1  A: 

Using the naked mail function is just asking for trouble ( http://en.wikipedia.org/wiki/E-mail_injection , php specific info: http://www.damonkohler.com/2008/12/email-injection.html ), and prevents simple debugging. I suggest you use an object wrapper around the mail function, both because this has benefits when you filter the headers, by making it a non-standard target for php mail form header injection spammers, and by allowing you to debug the messages easier by just dumping the created mail object and reviewing it's contents. For debugging it also allows you to provide a "just echo out the mail at the end" alternative for local testing on machines where you don't have/don't want to have a mail server, and don't want to even try to send out mail while you're just testing functionality.

Here is a wrapper (freely available for modification and use) that I created and use myself: http://github.com/tchalvak/ninjawars/blob/master/deploy/lib/obj/Nmail.class.php

Alternatively just check out PEAR mail: http://pear.php.net/package/Mail/

Tchalvak
Err, just to note, the Nmail class that I mention doesn't have the necessary user-input-validation stuff built in in that version, so you'll have to create those yourself.
Tchalvak
Added some naive blacklist filtering to that example, but I'd say just go with using pear mail. :D
Tchalvak
A: 

It's not jumping out at me, why don't you try turning the errors all the way up and see if that works.

error_reporting(1);

At the top of the script.

flaxeater
A: 

EDIT: Sorry, I see now you do have error reporting turned on. Make sure your INI file is set properly too. Try removing the ^ E_NOTICE so that you see those warnings, too.

I've had problems where mail() wouldn't say anything at all (and it would execute as if successful) when it didn't really. If you're bent on using mail(), you can use SwiftMailer, which generally throws helpful exceptions when something goes awry and includes a transport class Swift_MailTransport which uses mail() but is all dressed up in a nice object-oriented interface.

Andrew Noyes
A: 

So, due to the nature of the problem (mail is being accepted for delivery - $mail is true), the problem is likely in the message content. Do you have access to the mail server itself? Can you check the logs? var_dump() the $subject, $message, and set the headers to a var and var_dump() that as well. Examine the contents with a fine tooth comb. Remove suspect characters and line breaks until it does work.

One thing to try... (though, the fact that your other mail is being accepted says this is likely not the case)

http://www.php.net/manual/en/function.mail.php

If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

The problem with mail() is that its just feeding mail to the local sendmail daemon. It doesn't give you any active feedback on the mail, and the relay headers sometimes get you spam de-rated.

I'd check out http://sourceforge.net/projects/phpmailer/

Josh
A: 

Try wrapping lines in the message to 70 characters with $message = wordwrap($message, 70); Try replacing \r\n in the additional headers with \n in case your mail function is replacing \n for \r\n and you're ending-up with \r\r\n

jah
A: 

3:20pm and I just opened my email. Guess what, its flooded with emails. Must have been some kind of delay.

Thanks for the responses.

laron.ramsden