Results 1 to 2 of 2

Thread: Sendmail form

  1. #1
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Sendmail form

    Hi all,

    Can anyone shed some light on this one: when I fill in the form and send it via the browser works fine and I receive the message, but when I just click send without entering any data it prompts me to enter the unfilled fields specified, but still sends an empty email. Is there a way to fix this?

    Thanks for any help and suggestions.

    Code:
    <?php
    
    // process the email
    if (array_key_exists('submit', $_POST)) {
      ini_set("sendmail_from", "info@email.com");
    
    $to = 'info@email.com';
    $subject = 'Feedback from website.';
    // send email 
    $message = 	'Name: ' . $_REQUEST['name'] . "\n\n" . 
    			'Email: ' . $_REQUEST['email'] . "\n\n" . 
    			'Contact No.: ' . $_REQUEST['tel'] . "\n\n" . 
    			'Message: ' . $_REQUEST['feedback'];
    
    $email = $_REQUEST['email'];
    $headers = 'From: ' . $email . "\r\n" .
    'Reply-To: ' . $email . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    
    mail ($to, $subject, $message, $headers, "-finfo@email.com");
    
      $to = 'info@email.com'; // use your own email address
      $subject = 'Feedback from website!';
      
      // list expected fields
      $expected = array('name', 'email', 'tel', 'feedback');
      // set required fields
      $required = array('name', 'email', 'tel');
      // create empty array for any missing fields
      $missing = array();
      
      // 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);
    	  }
    	// otherwise, assign to a variable of the same name as $key
    	elseif (in_array($key, $expected)) {
    	  ${$key} = $temp;
    	  }
    	}
    
    	// validate the email address
    	if (!empty($email)) {
    
    	// regex to ensure no illegal characters in email address
    	$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
    
    	// reject the email address if it doesn't match
    	if (!preg_match($checkEmail, $email)) {
    	array_push($missing, 'email');
    	}
    	}
     
      // go ahead only if all required fields OK
      if (empty($missing)) {
        // build the message
        $message = "Name: $name\n\n";
        $message .= "Email: $email\n\n";
    	$message .= "Contact No.: $tel\n\n";
    	$message .= "Message: $feedback\n\n";
    
        // limit line length to 70 characters
        //$message = wordwrap($message, 70);
      
        // send it  
        $sendmail_from = mail($to, $subject, $message);
    	if ($sendmail_from) {
    	  // $missing is no longer needed if the email is sent, so unset it
    	  unset($missing);
    	  }
        }
      }
    ?>
        <?php
    		if ($_POST && isset($missing)) {
    ?>
        <p class="main_warning">Please complete the missing item(s) indicated. Your message has not been sent.</p>
        <?php
    		  }
    		elseif ($_POST && !$sendmail_from) {
    ?>
        <p class="main_warning">Sorry, there was a problem sending your message. Please try later.</p>
        <?php
    		  }
    		elseif ($_POST && $sendmail_from) {
    ?>
        <span class="confmsg"><h2>Thank you for your enquiry. We will reply to you as soon as possible.</h2></span>
    
    <?php } ?>
    
    <form id="contact" name="contact" method="post" action="">
    
          <?php
    	if (isset($missing) && in_array('name', $missing)) { ?>
          <span class="warning">Enter your name.</span>
          <?php } ?>
    
          <label><abbr title="Enter your name."><font color="#990000"><sup>*</sup></font>Name:</abbr>
            <input type="text" name="name" id="name" maxlength="35" />
          </label>
    
          <?php
    	if (isset($missing) && in_array('email', $missing)) { ?>
          <span class="warning">Enter your email address.</span>
          <?php } ?>
    
          <label><abbr title="Enter your email address."><font color="#990000"><sup>*</sup></font>Email:</abbr>
            <input type="text" name="email" id="email" maxlength="40"/>
          </label>
    
          <?php
    	if (isset($missing) && in_array('tel', $missing)) { ?>
          <span class="warning">Enter your contact no.</span>
          <?php } ?>
    
          <label><abbr title="Enter your contact telephone no."><font color="#990000"><sup>*</sup></font>Tel.:</abbr>
            <input type="text" name="tel" id="tel" maxlength="25" /></label>
    
    	  <label>&nbsp;<abbr title="Enter your message.">Message:</abbr></label>
    
      	 <textarea name="feedback" id="message" cols="0" rows="3"></textarea>
    
            <input type="submit" name="submit" id="send" class="submit" value="Send" title="Send" />
    
            <input type="reset" name="reset" id="reset" class="reset" value="Reset" title="Reset" />
    
        </form>
    Last edited by john0611; 12-15-2009 at 03:18 PM.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Your only checking if the submit is set to send the email
    "if (array_key_exists('submit', $_POST)) {"

    instead either add in if ((array_key_exists('submit', $_POST)) && (isset($missing) && in_array('email', $missing)) && etc.) { (not sure if brackets correct there just an example.

    then process email or do the validation through java script.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    john0611 (12-15-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •