Jump to content

Contact form working, but email not sending or being received


mike3075

Recommended Posts

I really need some help.  I am very new to PHP and I've been stuck for a couple of days.  I created a contact form and everything seems to be working correctly, except the mail is showing it's sent, but I never receive an email in my Inbox or Spam folder.  Can someone please review my code?

Here is the PHP:

<?php
  // DEFINE VARIABLE AND SET EMPTY VALUES
  $varfnameErr = $varlnameErr = $varemailErr = $varphoneErr = $varpositionErr = "";
  $varfname = $varlname = $varemail = $varphone = $varposition = $success = "";

  
  //FORM SUBMITTED WITH POST METHOD
  if ($_SERVER["REQUEST_METHOD"] == "POST") {

    //VALIDATE FIRST NAME
    if (empty($_POST["varfname"])) {
      $varfnameErr = "First Name is required";
    } else {
      $varfname = test_input($_POST["varfname"]);
      // MAKE SURE FIRST NAME ONLY CONTAINS LETTERS AND WHITE SPACE
      if (!preg_match("/^[a-zA-Z ]*$/",$varfname)) {
        $varfnameErr = "Only letters and white space are allowed";
      }
    }

    //VALIDATE LAST NAME
    if (empty($_POST["varlname"])) {
      $varlnameErr = "Last Name is required";
    } else {
      $varlname = test_input($_POST["varlname"]);
      // MAKE SURE LAST NAME ONLY CONTAINS LETTERS AND WHITE SPACE
      if (!preg_match("/^[a-zA-Z ]*$/",$varlname)) {
        $varlnameErr = "Only letters and white space are allowed";
      }
    }
  
    //VALIDATE EMAIL ADDRESS
    if (empty($_POST["varemail"])) {
      $varemailErr = "Email Address is required";
    } else {
      $varemail = test_input($_POST["varemail"]);
      // MAKE SURE EMAIL ADDRESS IS FORMATTED CORRECTLY
      if (!filter_var($varemail, FILTER_VALIDATE_EMAIL)) {
        $varemailErr = "Invalid email address format";
      }
    }

    //VALIDATE PHONE NUMBER
    if (empty($_POST["varphone"])) {
      $varphoneErr = "Phone number is required";
    } else {
      $varphone = test_input($_POST["varphone"]);
      // MAKE SURE PHONE NUMBER IS IN CORRECT FORMAT
      if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$varphone)) {
        $varphoneErr = "Invalid telephone format";
      }
    }
	
    //VALIDATE POSITION
    if (empty($_POST["varposition"])) {
      $varpositionErr = "Position is required";
    } else {
      $varposition = test_input($_POST["varposition"]);
    }

    //IF ALL DATA IS CORRECT
    if ($varfnameErr == '' and $varlnameErr == '' and $varemailErr == '' and $varphoneErr == '' and $varpositionErr == '') {
      $message_body = '';
      unset($_POST['submit']);

      //THIS IS JUST FOR TESTING PURPOSES
      $message_body = $varfname;

      //foreach ($_POST as $key => $value) {
      //    $message_body .= "$key: $value\n";
      //}
      $to = 'mygmail@gmail.com';
      $subject = 'Volunteer Form Submission';
      
      $message = wordwrap($message_body, 70);
      $headers  = 'MIME-Version: 1.0' . "\r\n"; 
      $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
      $headers .= "From: " . $varemail . "\r\n";
      $subject = 'Volunteer Form Submission';
      $to = 'mygmail@gmail.com';
      $result = mail($to, $subject, $message, $headers);
      //THIS RETURNS "1"
      print $result;
      
      //IF ALL DATA IS CORRECT MAKE SURE EMAIL WAS SENT
      if (mail($to, $subject, $message, $headers)) {
        $success = "Message sent. Thank you contacting us! We will reply as soon as possible.";
        $varfname = $varlname = $varemail = $varphone = $varposition = "";
      } else {
        $success = "Something went wrong!";
      }
    }
  }

//STRIP UNWANTED CHARACTERS FROM VARIABLES
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

 

Here is the HTML:

    <div class="row regformrow">
      <div class="coffee-span-12 regformcolumn">
        <div class="subgrid regformsubgrid">
          <div class="row regformsubgridheaderrow">
            <div class="coffee-span-12 regformsubgridheadercolumn">
              <h4 class="regformsubgridheadertitle">Volunteer Form</h4>
            </div>
          </div>
          <div class="row regformsubgridrow">
            <form class="form-container regformsubgridformcontainer" action=<?= $_SERVER['PHP_SELF']; ?> method="post">
              <div class="coffee-span-4 coffee-972-span-6 coffee-458-span-12 regformsubgridcolumn1">
                <div class="container regformsubgridcontainer1">
                  <label class="label form-label-all"><span style="font-style:italic;color:#FF5454;">* Required Fields</span></label></br>
                  <div class="formfieldgroup">
                    <input class="fname" name="varfname" type="text" tabindex="1" placeholder="First Name" value="<?= $varfname; ?>"><span class="requiredfield"> *</span>
                    <span class="form-error"><?= $varfnameErr ?></span>
                  </div>
                  <div class="formfieldgroup">
                    <input class="lname" name="varlname" type="text" tabindex="2" placeholder="Last Name" value="<?= $varlname; ?>"><span class="requiredfield"> *</span>
                    <span class="form-error"><?= $varlnameErr ?></span>
                  </div>
                  <div class="formfieldgroup">
                    <input class="email" name="varemail" type="text" tabindex="2" placeholder="Email Address" value="<?= $varemail; ?>"><span class="requiredfield"> *</span>
                    <span class="form-error"><?= $varemailErr ?></span>
                  </div>
                  <div class="formfieldgroup">
                    <input class="phone" name="varphone" type="text" tabindex="2" placeholder="Telephone Number" value="<?= $varphone; ?>"><span class="requiredfield"> *</span>
                    <span class="form-error"><?= $varphoneErr ?></span>
                  </div>
                  <div class="formfieldgroup">
                    <select class="select selectbox" name="varposition" id="varposition" tabindex="5"><option value="">Select one...</option><option value="Chaperone">Chaperone</option><option value="Class Monitor">Class Monitor</option><option value="Parking Attendant">Parking Attendant</option><option value="Party Coordinator">Party Coordinator</option><option value="Teacher Aid">Teacher Aid</option></select><span class="requiredfield"> *</span>
                    <span class="form-error"><?= $varpositionErr ?></span>
                  </div>
                  <div class="formfieldgroup">
                    <input class="test" name="test" type="text" tabindex="6" placeholder="If you are human, leave this field blank" value="">
                  </div>
                  <div class="formfieldgroup">
                    <button type="submit" class="button-submit-1" name="submit" tabindex="7" data-submit="...Sendng">Submit</button>
                  </div>
                  <div class="formfieldgroup">
  				  <div class="success"><?= $success; ?></div>
                  </div>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

 

Edited by mike3075
Link to comment
Share on other sites

HTH!

And - switching to phpmailer is a very smart move.  The old PHP mail() function didn't really give you a good result when there were errors.  PHPmailer is much better and much easier to use when you want to do different things with emails like adding attachments.

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.