1

I'm working on a website for a client who, of course, requires a functioning contact form. My PHP skills are pretty weak, so instead of driving myself crazy with the multitude of examples, I thought I'd ask for help directly.

Here's my form built using Bootstrap 4 framework:

<form id="contactForm" method="post" action="contact-form.php" role="form">
  <div class="messages"></div>
  <div class="controls"></div>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="clientName">Your Name *</label>
      <input class="form-control" id="clientName" type="text" placeholder="Enter Your Name" required="required" data-error="Your name is required." />
      <div class="help-block with-errors"></div>
    </div>
    <div class="form-group col-md-6">
      <label for="petName">Your Pet's Name *</label>
      <input class="form-control" id="petName" type="text" placeholder="Pet Name" required="required" data-error="Your pet's name is required." />
      <div class="help-block with-errors"></div>
    </div>
  </div>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="email">Email Address *</label>
      <input class="form-control id="email" type="email" placeholder="you@abc.def" required="required" data-error="Your email address is required.">
      <div class="help-block with-errors"></div>
    </div>
    <div class="form-group col-md-6">
      <label for="phone">Telephone Number</label>
      <input class="form-control" id="phone" type="text" placeholder="123-456-7890" />
    </div>
  </div>
  <div class="form-group">
    <label for="message">Message</label>
    <textarea class="form-control id="message" placeholder="Include any message here." rows="4" />
  </div>
  <div class="form-row">
    <div class="form-group col-md-6>
      <input class="btn btn-success" type="submit" value="Send Message" >
    </div>
  </div>
</form>

And the PHP that doesn't seem to work (except for the redirect. That works fine):

<?php
  // If the client name and email don't match
  if($_POST['clientName'] != $_POST['email']){
    $youremail = 'myemail@domain.com';
    $body = "New Appointment Request
    <hr>
    From: $_POST[clientName]
    Pet Name: $_POST[petName]
    E-Mail: $_POST[email]
    Phone: $_POST[phone]
    Message: $_POST[message]";
    if( $_POST['email'] && !preg_match( "/[\r\n]/", $_POST['email']) ) {
      $headers = "From: $_POST[email]";
    } else {
      $headers = "From: $youremail";
    }
    mail($youremail, 'New Message', $body, $headers );
  }
  header('Location:contact-redir.html');
?>

Am I missing a crucial detail to make this work? Or is the mail() function too old/insecure for modern mail clients?

Chirag
  • 363
  • 2
  • 12
Shawn Gray
  • 17
  • 5
  • 1
    The `mail()` function requires you to have a mail server setup locally. You're probably better off just using `PHPMailer` or a similar library where you can specify your mail server. – IcedAnt Sep 18 '18 at 05:37
  • You can specify the mail server in the php.ini also. – phrogg Sep 18 '18 at 06:00

0 Answers0