Page 1 of 1

Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 4:44 pm
by jeweline
Hi All,

I'm still having problems with getting a contact form to submit! Any help/guidance would be very much appreciated. I've uploaded the form on 2 servers: 1) a testing server, and 2) the actual website. The sites are not hosted by the same company.

The problem now is that the form works on my testing server (Form located here: http://www.buypuresilverbullion.com/silverlinemarket2/contactus.php).

But the same form (that consists of the same files) won't work on the actual website located here:
(http://www.silverlinemarket.com/contactus.php)

Whenever I correctly fill out the form on the actual web site, I get the following error message:

[b]Warning: mail() [function.mail]: SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in D:\Hosting\4750285\html\includes\process_mail.inc.php on line 100[/b]

This message is referring to some kind of problem with carriage returns and line feeds when the message is building.

Here's the php statement that builds the body of the e-mail message:

Code: Select all

// add label and value to the message body
       $message .= "ucfirst($item): $val\r\n\r\n";
 

Here's all the code in the include file, process_mail.inc.php:

Code: Select all

<?php
if (isset($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['SCRIPT_NAME'], '.inc.php')) exit;
 
// remove escape characters from POST array
if (PHP_VERSION < 6 && get_magic_quotes_gpc()) {
  function stripslashes_deep($value) {
    $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
    return $value;
  }
  $_POST = array_map('stripslashes_deep', $_POST);
}
 
// assume that there is nothing suspect
  $suspect = false;
  // create a pattern to locate suspect phrases
  $pattern = '/Content-Type:|Bcc:|Cc:/i';
  
  // function to check for suspect phrases
  function isSuspect($val, $pattern, &$suspect) {
    // if the variable is an array, loop through each element
    // and pass it recursively back to the same function
    if (is_array($val)) {
      foreach ($val as $item) {
         isSuspect($item, $pattern, $suspect);
      }
    } else {
      // if one of the suspect phrases is found, set Boolean to true
      if (preg_match($pattern, $val)) {
        $suspect = true;
      }
    }
  }
 
  // check the $_POST array and any subarrays for suspect content
  isSuspect($_POST, $pattern, $suspect);
 
  if ($suspect) {
    $mailSent = false;
    unset($missing);
  } else {
    // process the $_POST variables
    foreach ($_POST as $key => $value) {
      // assign to temporary variable and strip whitespace if not an array
      $temp = is_array($value) ? $value : trim($value);
      // if empty and required, add to $missing array
      if (empty($temp) && in_array($key, $required)) {
        array_push($missing, $key);
      } elseif (in_array($key, $expected)) {
        // otherwise, assign to a variable of the same name as $key
        ${$key} = $temp;
      }
    }
  }
  
  // validate the email address
  if (!empty($email)) {
    // regex to identify illegal characters in email address
    $checkEmail = '/^[^@]+@[^\s\r\n\'";, %]+$/';
    // reject the email address if it doesn't match
    if (!preg_match($checkEmail, $email)) {
      $suspect = true;
      $mailSent = false;
      unset($missing);
    }
  }
 
  
  // go ahead only if not suspect and all required fields OK
  if (!$suspect && empty($missing)) {
  
     // initialize the $message variable
     $message = '';
     // loop through the $expected array
     foreach($expected as $item) {
       // assign the value of the current item to $val
       if (isset(${$item}) && !empty(${$item})) {
         $val = ${$item};
       } else {
         // if it has no value, assign 'Not selected'
         $val = 'Not selected';
       }
       // if an array, expand as comma-separated string
       if (is_array($val)) {
         $val = implode(', ', $val);
       }
       [b]// add label and value to the message body
       $message .= "ucfirst($item): $val\r\n\r\n";[/b]
     }
 
    // limit line length to 70 characters
    $message = wordwrap($message, 70);
 
     // create Reply-To header
     if (!empty($email)) {
       $headers .= "\r\nReply-To: $email";
     }
 
     
    // send it  
    $mailSent = mail($to, $subject, $message, $headers);
    if ($mailSent) {
        // $missing is no longer needed if the email is sent, so unset it
        unset ($missing);
    }
  }
?>

Re: Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 5:32 pm
by Ollie Saunders
I know almost nothing about email. But if you're on a linux server you'll want to leave out \r.

Re: Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 6:04 pm
by jeweline
Thanks Ollie,

the site's being hosted on a windows box.

Re: Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 6:16 pm
by Ollie Saunders
I'd listen to McInfo.

Re: Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 6:18 pm
by jeweline
McInfo,

Thanks for taking a look.

I have limited the line length to 70 characters. The statement I'm using is:

Code: Select all

// limit line length to 70 characters
    $message = wordwrap($message, 70);

The statement I'm using to build the body of the message is as follows:

Code: Select all

// add label and value to the message body
       $message .= "ucfirst($item): $val\r\n\r\n";


For headers, I'm using the following statement:

Code: Select all

$headers .= "\r\nReply-To: $email";


A larger snippet of the code I'm using is:

Code: Select all

 // add label and value to the message body
       $message .= "ucfirst($item): $val\r\n\r\n";
     }
 
    // limit line length to 70 characters
    $message = wordwrap($message, 70);
 
     // create Reply-To header
     if (!empty($email)) {
       $headers .= "\r\nReply-To: $email";
     }
 
     
    // send it  
    $mailSent = mail($to, $subject, $message, $headers);
    if ($mailSent) {
        // $missing is no longer needed if the email is sent, so unset it
        unset ($missing);
    }

Re: Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 6:22 pm
by jeweline
I changed the $header and $message statements to include only the line feed (/n) instead of (\r\n\):

Code: Select all

 // add label and value to the message body
       $message .= "ucfirst($item): $val\n\n";
     }
 
    // limit line length to 70 characters
    $message = wordwrap($message, 70);
 
     // create Reply-To header
     if (!empty($email)) {
       $headers .= "\nReply-To: $email";
     }
 
And I'm still getting the following error message when the form is submitted:

Code: Select all

Warning: mail() [function.mail]: SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in D:\Hosting\4750285\html\includes\process_mail.inc.php on line 100
          // send it    var_dump($headers); //this line is just for testing purposes    $mailSent = mail($to, $subject, $message, $headers);    if ($mailSent) {        // $missing is no longer needed if the email is sent, so unset it        unset ($missing);    }

Re: Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 6:28 pm
by Eric!
If you want to experiment here are some statements that you can use to filter all your strings that are passed to mail(). I've never worked with qmail, but according to the link generated by the error, it wants to see \r\n for the fields.

Try this experiment to change all your strings in your fields before mail():

Code: Select all

$a = preg_replace("/(?<!\\n)\\r+(?!\\n)/", "\r\n", $a); //replace just CR with CRLF 
$a = preg_replace("/(?<!\\r)\\n+(?!\\r)/", "\r\n", $a); //replace just LF with CRLF 
$a = preg_replace("/(?<!\\r)\\n\\r+(?!\\n)/", "\r\n", $a); //replace misordered LFCR
Also note that the function wordwrap() by default uses just \n and there could be other places in your strings that need to be cleaned up. So if the preg_replace works, you know you'll need to go back through your code and clean up the fields properly.

Code: Select all

$message=wordwrap($message, 70, "\r\n");// use EOL

Re: Form Submission Working on 1 Server But Not on Another

Posted: Sun Sep 13, 2009 8:17 pm
by jeweline
Eric! :D

It's working! The last piece of code you gave me about cleaning up the wordwrap() string seems to have been the last piece needed.

With my practically non-existent understanding of PHP, I was following several exercises in a book by David Powers called, "The Essential Guide to Dreamweaver CS4 with CSS, AJAX, and Php" All I could figure out was that

Code: Select all

$message .= "ucfirst($item): $val\r\n\r\n";
wasn't right, and I tried to fix it. But I still had it wrong. A member on another board saw that it was wrong, and that it should have read:

Code: Select all

// add label and value to the message body
       $message .= ucfirst($item).": ".$val."\r\n\r\n";

Thank all of y'all so much. I definitely could not have gotten it working without your help. Thanks again! Cynthia :D

Re: Form Submission Working on 1 Server But Not on Another

Posted: Mon Sep 14, 2009 9:17 am
by Eric!
No problem. Sometimes differences between servers can be very hard to understand and fix.