Jason Riddle
Jason Riddle

Reputation: 11

I cannot get my contact.php to send any results to my email

The below is my contact.php form as well as the snip from the form code on the site. The issue is nothing is sent from the form to my email as desired. And the default validation against blank entries does not work. This is an appstrap site hosted on GoDaddy.

I am not sure why nothing is submitted to my email. Thoughts?

contact.php

<?php
/**
 * PHP Contact form
 *
 * Add basic form validation: required fields & email address valid
 * Sends email to
 */
$email_to = '[email protected]'; // update to your email
$email_to_name = '[email protected]'; // update to your name
$form_message = '<p>Please use the form below to contact us.</p>';
$form_errors = array();
$form_values = $_POST; // Optionally change to $_GET & <form action="contact.php" method="get"> on form
$required_fields = array( // if any of these fields are blank are error will show
  'contact-name' => 'Name',
  'contact-email' => 'Email',
  'contact-message' => 'Message',
);

if (!empty($form_values)) {
  // Form was submitted, validate required fields
  if (!empty($required_fields)) {
    foreach ($required_fields as $required => $label) {
      if (empty($form_values[$required])) {
        $form_errors[$required] = "The $label field is required and cannot be left blank."; //Message to show use
      }
    }
  }

  if (!empty($form_values['contact-email'])) {
    // Email address submitted, validate it
    // Remove all illegal characters from email
    $email = filter_var($form_values['contact-email'], FILTER_SANITIZE_EMAIL);

    // Validate e-mail
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
      // Valid, send email
      require 'assets/plugins/PHPMailer/PHPMailerAutoload.php';
      $mail = new PHPMailer;
      $mail->setFrom($email, $form_values['contact-name']);
      $mail->addAddress($email_to, $email_name);
      $mail->Subject = 'Contact form enquiry from ' . $form_values['contact-name'];
      $email_body = array();
      $email_body[] = '<strong>Email:</strong> ' . $form_values['contact-email'] .'<br />';
      $email_body[] = '<strong>Name:</strong> ' . $form_values['contact-name'] .'<br />';
      $email_body[] = '<strong>Message:</strong> ' . $form_values['contact-message'] .'<br />';
      $email_body[] = 'Email sent at '. date('r', time());
      $mail->msgHTML(implode('<br />', $email_body));

      if (!$mail->send()) {
        $form_errors[] = 'Error in sendng enquiry please email us at ' . $email_to . ' instead.';
      }
      else {
        $form_message = '<div class="alert alert-success" role="alert"><strong>Enquiry Successfully sent!</strong> Thank you we will get back to you as soon as possible.</div>';
      }      
    }
    else {
      // Invalid
      $form_errors['contact-email'] = 'The email address inputted is invalid.';
    }
  }

  // Make errors into message to user
  if (!empty($form_errors)) {
    $form_message = '<div class="alert alert-danger" role="alert"><strong>Error!</strong> The following errors have occurred submitting the form: <hr />' . implode('<br />', $form_errors) .'</div>';
  }  
}
?>

HTML Form

<form id="contact-form" action="contact.php" role="form" method="POST">
  <div class="form-group" data-animate="fadeInDown" data-animate-delay="0.2">
    <label class="sr-only" for="contact-name">Name</label>
    <input type="text" class="form-control" id="contact-name" placeholder="Name">
  </div>
  <div class="form-group" data-animate="fadeInDown" data-animate-delay="0.3">
    <label class="sr-only" for="contact-email">Email</label>
    <input type="email" class="form-control" id="contact-email" placeholder="Email">
  </div>
  <div class="form-group" data-animate="fadeInDown" data-animate-delay="0.4">
    <label class="sr-only" for="contact-message">Message</label>
    <textarea rows="5" class="form-control" id="contact-message" placeholder="Message"></textarea>
  </div>
  <input type="submit" class="btn btn-outline-primary btn-lg" value="Send Message">
</form>

Upvotes: 1

Views: 42

Answers (1)

Ray A
Ray A

Reputation: 1351

In the HTML form, you are not setting the inputs names, you are just assigning them to Ids, for example:

You have <input type="text" class="form-control" id="contact-name" placeholder="Name"> There is one attribute name is missing, it is the name that PHP will use to get the input value, so when your code executes if (!empty($form_values['contact-email'])), it will return false, since you don't have any input with name contact-email, try the below form and it should work.

<form id="contact-form" action="contact.php" role="form" method="POST">
  <div class="form-group" data-animate="fadeInDown" data-animate-delay="0.2">
    <label class="sr-only" for="contact-name">Name</label>
    <input type="text" class="form-control" id="contact-name" placeholder="Name" name="contact-name">
  </div>
  <div class="form-group" data-animate="fadeInDown" data-animate-delay="0.3">
    <label class="sr-only" for="contact-email">Email</label>
    <input type="email" class="form-control" id="contact-email" name="contact-email" placeholder="Email">
  </div>
  <div class="form-group" data-animate="fadeInDown" data-animate-delay="0.4">
    <label class="sr-only" for="contact-message">Message</label>
    <textarea rows="5" class="form-control" id="contact-message" placeholder="Message" name="contact-message"></textarea>
  </div>
  <input type="submit" class="btn btn-outline-primary btn-lg" value="Send Message">
</form>

Upvotes: 1

Related Questions