Skip to content
Advertisement

Why is my PHP Email Form sending a blank email

Let me first explain my problem.

I had a freelance job where I was designing & developing a website for a business company. I coded an email form and attached it to a PHP sending form, it gathers the Name, Phone, Email, Subject, and Message of whoever fills out the form and sends it to the owner of the business.

Recently the owner has been complaining about receiving blank emails and thinking they are missing business I went back into the PHP code and added a verification system, added even a redirect that when it submits properly it will redirect to a page that has 2 options either “Sorry there has been a Problem” or “thank you for sending the email”

I also went into my HTML and fixed that as well adding “required fields” so there is absolutely no way the email can be sent without things filled in.

Can someone please explain a sure-fire way to explain it because the owner is still complaining about getting blank emails…

This is the HTML CONTACT FORM FILE

    <form method="post" action="acknowledge.php"  role="form" class="php-email-form">
      <div class="form-row section-bg">
        <div class="col-md-6 form-group">
          <input type="text" name="name" class="form-control" id="name" placeholder="Your Full Name" data-rule="minlen:4" data-msg="Please enter a valid Full Name" required/>
          <div class="validate"></div>
        </div>
        <div class="col-md-6 form-group">
          <input type="text" class="form-control" name="cname" id="cname" placeholder="Company Name" data-rule="minlen:4" data-msg="Please enter a valid Company Name" required/>
          <div class="validate"></div>
        </div>
        <div class="col-md-6 form-group">
          <input type="text" class="form-control" name="tel" id="tel" placeholder="Telephone" data-rule="minlen:4" data-msg="Please enter a valid Phone Number" required/>
          <div class="validate"></div>
        </div>
        <div class="col-md-6 form-group">
          <input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid Email" required/>
          <div class="validate"></div>
        </div>
      </div>
      <div class="form-group section-bg">
        <textarea class="form-control" name="message" id="message" rows="5" data-rule="required" placeholder="Message"  required></textarea>
      </div>
      <input type="submit" class="form-control" name="send" value="Send Message"/>
    </form>

This is the PHP EMAIL FILE

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (isset($_POST['send'])) {
     $to = 'Owners Email';
     $subject = 'Business Inquiries';
     $name = 'Full Name: ' . $_POST['name'] . "rnrn";
     $cname = 'Company Name: ' . $_POST['cname'] . "rnrn";
     $tel = 'Telephone Number: ' . $_POST['tel'] . "rnrn";
     $emails = 'Email: ' . $_POST['email'] . "rnrn";
     $message = 'Message: ' . $_POST['message'];
     
     $email_from = 'Owners business website name';
    
     $email_body = "$namen $cnamen $emailsn $teln $messagen";

     $headers = "From: $email_from rn";
     $headers .= 'Content-Type: text/plain; charset=utf-8';
    
     $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
     if($email) {
         $headers .= "rnReply-To: $emails";
     }


     $success = mail($to, $subject, $email_body, $headers);
}

?>
<body>
<?php if (isset($success) && $success) { ?>
<h1>Thank You!</h1>
Your Message has been sent!
<?php } else { ?>
<h1>Oops!</h1>
Sorry, there was a problem sending your message. Please Try Again.
<?php } ?>
</body>

Any help would be appreciated

Advertisement

Answer

you have to check all the required fields to be set something like this, optionally you can check each field to be set.

   <?php
   
   $name = filter_input (INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
   $cname = filter_input (INPUT_POST, 'cname', FILTER_SANITIZE_SPECIAL_CHARS);
   $tel = filter_input (INPUT_POST, 'tel', FILTER_SANITIZE_NUMBER_INT);
   $message = filter_input (INPUT_POST, 'message', FILTER_SANITIZE_SPECIAL_CHARS);
   $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

   if (!empty($name) && !empty($cname) && !empty($tel) && !empty($message) ) {
        $to = 'Owners Email';
        $subject = 'Business Inquiries';
        $name = 'Full Name: ' . $name . "rnrn";
        $cname = 'Company Name: ' . $cname . "rnrn";
        $tel = 'Telephone Number: ' . $tel . "rnrn";
        $emails = 'Email: ' . $email . "rnrn";
        $message = 'Message: ' . $message;
        
        $email_from = 'Owners business website name';
       
        $email_body = "$namen $cnamen $emailsn $teln $messagen";
   
        $headers = "From: $email_from rn";
        $headers .= 'Content-Type: text/plain; charset=utf-8';
       
        
        if($email) {
            $headers .= "rnReply-To: $emails";
        }
   
   
        $success = mail($to, $subject, $email_body, $headers);
   }
   
   ?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement