Jump to content

[SOLVED] Need Help with PHP Statement to Build Body of E-mail Message


jeweline

Recommended Posts

Hi All,

 

I'm still having problems with getting a contact form to submit (I've attached the files to this post.) ! 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:

 // 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:

<?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);
}
  }
?>

 

[attachment deleted by admin]

Link to comment
Share on other sites

This is a maill header issue.  The MTA on your actual web site does not like the headers that php is sending to it.  Please put

var_dump($headers);

right above

$mailSent = mail($to, $subject, $message, $headers);

and then post the result.

 

Blindly, I can tell you that many MTA's do not process mail outbound for account that they do not host (i.e having php send an email as clown@college.com and the MTA doesn't have an account for clown@college.com). This is most likely the case if this is a third party host.

 

Link to comment
Share on other sites

Thanks!

 

I added the statement as you instructed. Here's the code I received:

 

rray(17) { ["First_Name"]=>  string(7) "Cynthia" ["Last_Name"]=>  string(9) "Owens-ali" ["company"]=>  string(0) "" ["Street_Address1"]=>  string(7) "asdfsdf" ["Street_Address2"]=>  string(4) "asfs" ["city"]=>  string( "asdfasdf" ["state_region"]=>  string(6) "asdfsd" ["Postal_Code"]=>  string(5) "asdfa" ["country_code"]=>  string(2) "JM" ["url"]=>  string(0) "" ["daytime_phone"]=>  string(12) "000-000-0000" ["evening_phone"]=>  string(0) "" ["email2"]=>  string(22) "jeweline1961@gmail.com" ["comments"]=>  string(135) "Matt, I'm still testing this form. If you receive this message, please forward it to me at jeweline1961@gmail.com. Thanks, Cynthia" ["prefer_to_be_contacted_by"]=>  string(5) "Phone" ["best_time_to_call"]=>  string(4) "Noon" ["send"]=>  string(22) "Send Us Your Comments!" }
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

 

 

Link to comment
Share on other sites

Ryan,

 

I'm over my head. I'm don't understand what you're telling me: :-\ :confused:

 

That may be your issue in of itself.  The header of the email should be a string that you pass to the mail() construct. According to your dump it appears that you are trying to pass an array in the header paramater for the mail() construct.

 

But the array is created from the following code, that appears at the very top of the form page which I was instructed to put there for debugging purposes since the form was not working:

I'll take it out, and repost any error messages I get. Thanks again!

 

<?php
// JUST FOR TESTING PURPOSES prints all form variables to the screen so you can see what's being created and it's value
var_dump($_POST); 
?>

 

Here's a snippet of the code for the form:

<?php
// JUST FOR TESTING PURPOSES prints all form variables to the screen so you can see what's being created and it's value
var_dump($_POST); 
?>
<?php
if (array_key_exists('send', $_POST)) {
// mail processing script
$to = 'contact@silverlinemarket.com';
$subject = 'Contact Form Inquiry from Silverline Market Website Visitor';

// list expected fields
  $expected = array('First_Name', 'Last_Name', 'company', 'Street_Address1', 'Street_Address2', 'city', 'state_region', 'Postal_Code',  'country_code', 'daytime_phone', 'evening_phone', 'email', 'comments', 'prefer_to_be_contacted_by', 'best_time_to_call');
//set required fields
  $required = array('First_Name', 'Last_Name', 'Street_Address1', 'city', 'state_region', 'Postal_Code', 'country_code', 'daytime_phone', 'comments', 'state');
  // create empty array for any missing fields
  $missing = array();
// create additional headers
     $headers = "From: Silverline Market Corporation<contact@silverlinemarket.com>\r\n";
 $headers .= 'Content-Type: text/plain; charset=utf-8';
 $process = 'includes/process_mail.inc.php';
  if (file_exists($process) && is_readable($process)) {
	include($process);
  } else {
	$mailSent = false;
	mail($to, 'Server problem', "$process cannot be read", $headers);
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>

 

 

Link to comment
Share on other sites

Ryan,

 

I deleted the "var_dump($_POST);" statement, and tried the form again.

 

When I clicked the submit button, I still got the following error message:

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

 

Link to comment
Share on other sites

Hi, this error message normally means an error due to new line formatting in the email "body"

 

I wonder if there is something in the wordwrap() function that is messing things up?

 

You have this:

$message = wordwrap($message, 70);

Can you post that function?

 

Also, it might be worth doing an echo on $message just to see what you actually have.

 

Chris

Link to comment
Share on other sites

Thanks Chris!

 

The only place in my code where I even have the wordwrap() function is just that one time. It doesn't appear anywhere else.

 

Here's a snippet of the code where the $message variable is initialized and the message is sent:

// 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);
       }
       // 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);
}

 

Link to comment
Share on other sites

Chris,

 

I'm not sure how to echo the message, but I added the following in an attempt to do so:

 

   // send it
echo $message; 
    $mailSent = mail($to, $subject, $message, $headers);
    if ($mailSent) {
	// $missing is no longer needed if the email is sent, so unset it
	unset ($missing);
}

 

Link to comment
Share on other sites

Chris,

 

Here's a link I found that explains the wordwrap() function:

http://us3.php.net/manual/en/function.wordwrap.php

 

and here's the code I got when I echoed the message. It looks like the first_name variable is being printed way to often:

 

ucfirst(First_Name): cynthia ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(country_code): US ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(country_code): US ucfirst(daytime_phone): 000-000-0000 ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(country_code): US ucfirst(daytime_phone): 000-000-0000 ucfirst(evening_phone): Not selected ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(country_code): US ucfirst(daytime_phone): 000-000-0000 ucfirst(evening_phone): Not selected ucfirst(email): Not selected ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(country_code): US ucfirst(daytime_phone): 000-000-0000 ucfirst(evening_phone): Not selected ucfirst(email): Not selected ucfirst(comments): Matt, if you receive this message, please forward it to me at jeweline1961@gmail.com. Thanks, Cynthia ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(country_code): US ucfirst(daytime_phone): 000-000-0000 ucfirst(evening_phone): Not selected ucfirst(email): Not selected ucfirst(comments): Matt, if you receive this message, please forward it to me at jeweline1961@gmail.com. Thanks, Cynthia ucfirst(prefer_to_be_contacted_by): Email ucfirst(First_Name): cynthia ucfirst(Last_Name): owens ucfirst(company): Not selected ucfirst(Street_Address1): 123 anywhere street ucfirst(Street_Address2): Not selected ucfirst(city): new york ucfirst(state_region): ny ucfirst(Postal_Code): 00000 ucfirst(country_code): US ucfirst(daytime_phone): 000-000-0000 ucfirst(evening_phone): Not selected ucfirst(email): Not selected ucfirst(comments): Matt, if you receive this message, please forward it to me at jeweline1961@gmail.com. Thanks, Cynthia ucfirst(prefer_to_be_contacted_by): Email ucfirst(best_time_to_call): Morning
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

 

 

Link to comment
Share on other sites

:D

 

The form is now working! And I want to extend my thanks and gratiude to all of y'all. I couldn't have fixed it without your help and guidance.

 

Chris,

 

There was a problem with the wordwrap() function as well as the strings I was passing to mail(). I also had my $message statement wrong. With the help of everybody between 3 boards, you all contributed to my being able to have a working form.

 

Here's the advice I got that corrected the problems:

 

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():

 

$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.

 

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

 

I'm guessing you're problem is here:

 

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

 

It should be

 

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

 

Thanks again!  :D

Cynthia

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.