user1604302
user1604302

Reputation: 1

PHP script to email form fields is not sending all fields and doesn't always send

I've been back and forth on many php email scripts with this, even purchasing Formtoemailpro (http://formtoemail.com/) and for now I'm going with this one. I did end up receiving one email from this script (and f2ep) but it only sent the labels and part of the name and the email address, not any of the other fields. When I checked with an echo, it did know what each variable was supposed to be defined as.

I'm using dreamhost as my hosting plan. I get similar results on every script I try - one or more fields in the email are displayed if any. The email I've been using to test is my personal gmail account but the email my client would like to use is one they set up with the new domain/hosting - I believe it's a google apps account.

Is there something basic that I'm getting wrong? I'm also open to improvements on the script.

html:

   <form name="form1" id="form1" method="post" action="formtest.php"> 
   <span class="formtext">

    <p>
      <label for="nameFirst">First Name:</label>   
      <input type="text" name="nameFirst" id="nameFirst" />
    </p>
    <p>
      <label for="nameLast">Last Name:</label>
      <input type="text" name="nameLast" id="nameLast" />
    </p>
    <p>
      <label for="email">E-mail: </label>
      <input type="text" name="email" id="email" />
    </p>
    <p>
      <label for="phone"> Phone:</label>
      <input type="text" name="phone" id="phone" />
    </p>
    <p>
      <label for="subject">Reason for contacting us?</label>
        <br />
      <select name="subject"  id="subject">
        <option>Request more information</option>
        <option>Recommend a project</option>
        <option>Suggest a resource</option>
        <option>Get us to help a community</option>
      </select>
    </p>
    <p>
      <label for="messagebox" class="formtext">Message:</label>
      <br>
      <textarea name="messagebox" id="messagebox" cols="45" rows="5"></textarea>
    </p>
    <p>
      <input type="submit" name="submit" id="button" value="Send" />
    </p>
  </span>
</form>

php:

<?php

  $to = "[email protected]";
  $subject = $_POST['subject'];
  $name = $_POST['nameFirst'].$_POST['nameLast'];
  $phone = $_POST['phone'];
  $email = $_POST['email'];
  $message = $_POST['messagebox'];


if(isset($_POST['submit'])) {

  $body = "From: {$name}\n Phone: {$phone}\n E-Mail: {$email}\n Message:\n {$message}";

  $headers = "From: ". $name . " <" . $email . ">\r\n";
  $headers .= "Reply-To:". $email ."\r\n";

  mail($to, $subject, $body, $headers);

  // redirect 
  header('Location: ThankYouContact.html');
  } 


// Error Checking http://bit.ly/NDhVrm
 /* if (@mail($to, $subject, $message)) { //@ suppresses error msg
    echo('<p>Mail sent successfully.</p>'); 
  } else { 
    echo('<p>Mail could not be sent.</p>'); 
  } */


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
     {
     return true;
   }
   else
     {
     return false;
   }
}

?>

Upvotes: 0

Views: 826

Answers (1)

Marc B
Marc B

Reputation: 360572

Your code is somewhat risky - you don't test for the existence of all those $_POST values before you use them, which may cause PHP to output warning messages. If those warnings are output, then your header() redirect will fail with the usual headers already sent error.

Some things to check:

a) the return value of mail(). Right now you're assuming it succeeded. You should test for a boolean false, indicated that something blew up between PHP and the SMTP server.

b) If dreamhost allows it or can provide it, check the mail server logs to see what happens there after PHP has handed over the email - remember, PHP's mail() is the equivalent of dropping an envelope in a mailbox - it does not actually deliver the mail.

c) You may want to consider switching to PHPMailer or SwiftMailer instead, both of which give you far better diagnostic messages in case there is trouble.

d) You should be doing if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... } instead of checking for a form field. The REQUEST_METHOD is 100% reliable, while checking for formfields depends on you not changing anything.

Upvotes: 3

Related Questions