Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mailer is not sending all the variables? 1

Status
Not open for further replies.

mRko81

Programmer
Apr 25, 2006
39
US
I have taken over this web site and the contact us page was working fine as far as i know but now it is not sending the Comments field.

Any ideas would really help.

Code:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/inc.config.php');
include('class.mailer.php');
require("class.inputfilter.php");
include('functions.php');

$errors = array();    //this array will store which inputs could not validate
                      //therefore $inputs_with_errors['title'] = "sometext" means the 
                      //title was invalid.
$email_sent = FALSE;
					  
if( isset($_POST['fm-submit']) ) {

    //process form and send mail if all is good
    if( check_Form($errors) ) {
        $email = new mailer('to@web.net','from','subject');
				$email->setBody( createBody() );
        $email->send();
				$email_sent = TRUE;   
    }  
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
<head>
	<title></title>

</head>
<body>

        <h1>Contact Us</h1>
        <?php if($email_sent) : ?>
        <p id="mailSent">Your message has been sent. Thank you for your interest.
          <a href="<?=$_SERVER['PHP_SELF']?>" title="Show the form">(I want to
          send another message)</a></p>
        <?php unset($_POST['fm-submit']);	?>
        <?php endif; ?>

        <?php if( !$email_sent ) : ?>
        <p id="formInstructions">Use the form below to send us a question or
          comment.<br />
          <span class="required">(*)</span> means required field</p>
        <form id="contactForm" action="<?= $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return validateForm()">
          <fieldset>
          <legend>Contact Form</legend>
          <?php include($_SERVER['DOCUMENT_ROOT'].'/assets/includes/inc.errorbox.php'); ?>
          <!-- name -->
          <div>
            <label for="fm-name"><span class="required">Name: (*)</span></label>
            <input type="text" name="fm-name" id="fm-name" value="<?= (isset($_POST['fm-name'])) ? $_POST['fm-name'] : '' ?>" size="30" class="requiredField" />
          </div>
          <!--phone-->
          <div>
            <label for="fm-phone">Phone Number:</label>
            <input type="text" name="fm-phone" id="fm-phone" value="<?= (isset($_POST['fm-phone'])) ? $_POST['fm-phone'] : '' ?>" size="14" maxlength="14" />
          </div>
          <!--email-->
          <div>
            <label for="fm-email">Email:</label>
            <input type="text" name="fm-email" id="fm-email" value="<?= (isset($_POST['fm-email'])) ? $_POST['fm-email'] : '' ?>" size="30" />
          </div>
          <!--additional comments-->
          <div>
            <label for="fm-comments"><span class="required">Comments: (*)</span></label>
            <textarea name="fm-comments" id="fm-comments" cols="50" rows="8" class="requiredField"><?= (isset($_POST['fm-comments'])) ? $_POST['fm-comments'] : '' ?>
</textarea>
          </div>
          <div id="submitBtnRow">
            <input type="submit" name="fm-submit" id="fm-submit" value="Submit" />
          </div>
          </fieldset>
        </form>
        <?php endif; ?>

</body>
</html>

<?php
/**
 * validates the form
 *
 * @param Array $errors The array which will hold any error messages
 * @returns Boolean
 */
function check_Form(&$errors) {
	//comments required
	//for users sake, check validity of email address and phone number if entered
	
	//clean input
	$myFilter = new InputFilter();
	$_POST = $myFilter->process($_POST);
	
	$valid = true;
	
	//phone number  (only validate if something was entered)
	$_POST['fm-phone'] = trim($_POST['fm-phone']);
	if( !empty($_POST['fm-phone']) ) {
        if( validate_USphone($_POST['fm-phone']) == false ) {				
		      $errors[] = array('id'=>'fm-phone','error'=>'Invalid phone number. Use format 555-555-5555' . "\n" . '(Note: Phone Number can be left blank)');
		      $valid = false;
	      }
	}
	
	//email address  (only validate if something was entered)
	$_POST['fm-email'] = trim($_POST['fm-email']);
	if( !empty($_POST['fm-email']) ) {
        if( validate_email($_POST['fm-email']) == false ) {
	    	$errors[] = array('id'=>'fm-email','error'=>'Invalid Email. Please re-enter.' . "\n" . '(Note: Email can be left blank)');
	    	$valid = false;
	    }
	}
    
	//name
	$_POST['fm-name'] = trim($_POST['fm-name']);
	if( empty($_POST['fm-name']) ) {
		$valid = FALSE;
		$errors[] = array('id'=>'fm-name','error'=>'Name');
	}	

	//comments
	$_POST['fm-comments'] = trim($_POST['fm-name']);
	if( empty($_POST['fm-comments']) ) {
		$valid = FALSE;
		$errors[] = array('id'=>'fm-comments','error'=>'Comments');
	}		
	
	return $valid;
}

/**
 * Creates the email body using the form fields 
 *
 * @returns String;
 */
function createBody() {
  $body = '';
  //name        
  $body .= 'Name: ';
	$body .= (empty($_POST['fm-name'])) ? 'NA' : $_POST['fm-name'];
	$body .= "\n\n";
     
  //contact
  $body .= 'Phone Number: ';
  $body .= (empty($_POST['fm-phone'])) ? 'NA' : $_POST['fm-phone'];
  $body .= "\n" . 'Email: ';
  $body .= ( empty($_POST['fm-email']) ) ? 'NA' : $_POST['fm-email'];
  $body .= "\n\n";
	             
  //comments
  $body .= 'Comments: ' . "\n";
  $body .= '------------------------------------------'."\n";
  $body .= ( empty($_POST['fm-comments']) ) ? 'NA' : $_POST['fm-comments'];
  $body .= "\n".'------------------------------------------'."\n\n";
        
  $body .= 'END OF SUBMISSION';
        
  return $body;
} //end function

/**
 * Strips leading and trailing spaces, then checks if the field is empty
 *
 * @param String $field_value The field being checked
 * @param Array $errors The array to hold the error messages
 * @param String $field_name The name of the field.
 * @return Boolean (True if field is empty, False if field is not empty)
 */
function fieldEmpty( &$field_value, &$errors, $field_name ) {
    $empty = false;

    $field_value = tidy($field_value);
    if( empty($field_value) ) {
        $errors[] = $field_name;
        $empty = true;
    }  
    
    return $empty;
} 
?>

class.mailer.php (this is incuded at the top)
Code:
<?php

/**
 * Simple class for sending email using the sendmail service. This class is very basic.
 *
 * example of usage:
 *      $email = new mailer('to@email.com','from@email.com','this is the subject');
 *      $email->setBody('Hello, this is the body of the email');
 *      $email->send();
 *
 */
class mailer {
 
    /**
	 * Who the email is being sent to
	 *
	 * @var String
	 * @access private
	 * @see mailer()
	 */    
    var $to;
    
    /**
     * Who the email is from
     *
     * @var String
     * @access private
     * @see mailer()
     */
    var $from;

    /**
     * The subject of the email
     *
     * @var String
     * @access private
     * @see mailer()
     */        
    var $subject;
    
    /**
     * The email body
     *
     * @var String
     * @access private
     * @see setBody()
     */
    var $body;

    
    
    /**
     * Constructor
     *
     * @access public
     * @param String $to Who the email is being sent to
     * @param String $from Who is sending the email
     * @param String $subject The subject of the email
     */
    function mailer( $to,$from,$subject) {
        $this->to = $to;
        $this->from = $from;
        $this->subject = $subject;
    }
    
    /** 
     * Sets the body of the email
     *
     * @param String $body The email body
     */
    function setBody( $body ) {
        $this->body = $body;
    }
    
    /**
     * Sends the email
     */
    function send() {
        //create header           
        $headers = 'From: '.$this->from. "\r\n" .
                   'MIME-Version: 1.0' . "\r\n" .
                   'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
        //send email                      
        mail( $this->to, $this->subject, $this->body, $headers );    
    }
       
    
} //end of class

Thats pretty much everything that i think is used in the send mail. I realy appreciate any help.
 
Would I be correct to assume that the "Comments" part of the emails all look like:

Comments:
------------------------------------------
NA
------------------------------------------

?





Want the best answers? Ask the best questions! TANSTAAFL!
 
actually it is coming through like:


Name: NAME

Phone Number: 111-111-1111
Email: name@web.com

Comments:
------------------------------------------
NAME
------------------------------------------

END OF SUBMISSION


It looks like it is writting the name variable ( fm-name ) instead of the comments ( fm-comments )
 
I simply do not see how it is possible that the script code you posted would write the name in both places. Are you sure this is the live copy of this file?




Want the best answers? Ask the best questions! TANSTAAFL!
 
I think I found the problem:

Code:
 //comments
[red]    $_POST['fm-comments'] = trim($_POST['fm-name']);[/red]
    if( empty($_POST['fm-comments']) ) {
        $valid = FALSE;
        $errors[] = array('id'=>'fm-comments','error'=>'Comments')

Its assigning the trimmed value of $_POST['fm-name'] to $_POST['fm-comments'] effectively overwriting whatever the comment used to be.

it should read:
Code:
 //comments
[red]    $_POST['fm-comments'] = trim($_POST['[green]fm-comments[/green]']);[/red]
    if( empty($_POST['fm-comments']) ) {
        $valid = FALSE;
        $errors[] = array('id'=>'fm-comments','error'=>'Comments')

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
YOU GUYS ROCK!
thank you so much for the quick replies here!

let me say it again,"YOU ROCK!
 
Glad I could help. Sometimes the errors just jump out at you, and other times you can't find them even if your life depended on it. This was one of the former cases.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top