0

HTML Code:

<form method="post" action="form.php">
    <div class="col-sm-6 form-group">
    <input class="form-control" id="name" name="name" type="text" placeholder="Name">
  </div>
  <div class="col-sm-6 form-group">
  <input class="form-control" id="email" name="email" type="email" placeholder="Email">
  </div>
  <div class="col-xs-12">
  <textarea class="form-control" id="message" name="message" type="message" rows="5" placeholder="Message"></textarea>
  </div>
  <button type="button" id="submit">Submit</button>
  <?php echo $result; ?>  
</form>

PHP Code:

<?php

if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Demo Form'; 
    $to = 'example@gmail.com'; 

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

    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You</div>';
        } else {
            $result='<div class="alert alert-danger">Please try again later</div>';
        }
    }
}
?>

So this is the code and I don't know why I still don't get mail when the form is submitted. I tried everything I know but is it still the same. Moreover, I am using a web hosting that supports send emails and PHP and more. And it still doesn't work.

Shaohao
  • 3,471
  • 7
  • 26
  • 45
PNinja
  • 59
  • 1
  • 2
  • 7

1 Answers1

0

There are multiple logical and syntactical issues there.

  1. You need to validate $_POST before you assign it to any variable.
  2. You are using $subject but you do not have any value in that variable.
  3. You are mixing header values to the mail body

I tried to fix issues try this.

<?php

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

    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Demo Form'; 
    $to = 'example@gmail.com'; 
    $body = $message;
    $subject="Demo message";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n".'X-Mailer: PHP/' . phpversion();


    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $headers)) {
            $result='<div class="alert alert-success">Thank You</div>';
        } else {
            $result='<div class="alert alert-danger">Please try again later</div>';
        }
    }
}

?>

For further details of refer this http://php.net/manual/en/function.mail.php

Manish Shukla
  • 1,355
  • 2
  • 8
  • 21