-1

I found a Bootstrap form on the internet and now I want to send it to my email. I have the form online, so no issues with localhost etc. When I try send it I get a message 'Thank You! I will be in touch'. So it sends? But I don't receive the email in my mailbox. Anyone an idea? See code below.

ps. When I run the code local he got some errors, see image. I don't have those errors online, but maybe that is the reason why it doesn't send anything?

enter image description here

<?php
 if (isset($_POST["submit"])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $human = intval($_POST['human']);
  $from = 'Demo Contact Form'; 
  $to = 'example@example.com'; 
  $subject = 'Message from Contact Demo ';
  
  $body ="From: $name\n E-Mail: $email\n Message:\n $message";
  // Check if name has been entered
  if (!$_POST['name']) {
   $errName = 'Please enter your name';
  }
  
  // Check if email has been entered and is valid
  if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
   $errEmail = 'Please enter a valid email address';
  }
  
  //Check if message has been entered
  if (!$_POST['message']) {
   $errMessage = 'Please enter your message';
  }
  //Check if simple anti-bot test is correct
  if ($human !== 5) {
   $errHuman = 'Your anti-spam is incorrect';
  }
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
 if (mail ($to, $subject, $body, $from)) {
  $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
 } else {
  $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
 }
}
 }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
    <meta name="author" content="BootstrapBay.com">
    <title>Bootstrap Contact Form With PHP Example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  </head>
  <body>
   <div class="container">
    <div class="row">
     <div class="col-md-6 col-md-offset-3">
      <h1 class="page-header text-center">Contact Form Example</h1>
    <form class="form-horizontal" role="form" method="post" action="mail.php">
     <div class="form-group">
      <label for="name" class="col-sm-2 control-label">Name</label>
      <div class="col-sm-10">
       <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
       <?php echo "<p class='text-danger'>$errName</p>";?>
      </div>
     </div>
     <div class="form-group">
      <label for="email" class="col-sm-2 control-label">Email</label>
      <div class="col-sm-10">
       <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
       <?php echo "<p class='text-danger'>$errEmail</p>";?>
      </div>
     </div>
     <div class="form-group">
      <label for="message" class="col-sm-2 control-label">Message</label>
      <div class="col-sm-10">
       <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
       <?php echo "<p class='text-danger'>$errMessage</p>";?>
      </div>
     </div>
     <div class="form-group">
      <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
      <div class="col-sm-10">
       <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
       <?php echo "<p class='text-danger'>$errHuman</p>";?>
      </div>
     </div>
     <div class="form-group">
      <div class="col-sm-10 col-sm-offset-2">
       <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
      </div>
     </div>
     <div class="form-group">
      <div class="col-sm-10 col-sm-offset-2">
       <?php echo $result; ?> 
      </div>
     </div>
    </form> 
   </div>
  </div>
 </div>   
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  </body>
</html>
neophyte
  • 6,540
  • 2
  • 28
  • 43
Johan
  • 23
  • 9
  • Unless you have any SMTP server configuration, any email send in localhost won't work. That's why you're having above exception. because your code halts at php mail() function. – CodeMonkey Feb 23 '17 at 12:13
  • I have a website running online where I can test the form. The image was to show what kind of errors there are on localhost, but I try to send the mail via a website. – Johan Feb 23 '17 at 12:18
  • You've the errors printed on page and yet you're unable to figure out the problem? – asprin Feb 23 '17 at 13:19

2 Answers2

0

Use this modified code to get rid of the initial localhost errors.

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email']; //Sender's email
    $message = $_POST['message'];
    $human = intval($_POST['human']);

    $to = "receivers@email.address";
    $subject = "Message from Contact Demo";
    $body = "From: $name\n";
    $body .= "E-Mail: $email\n";
    $body .= "Message:\n $message";
    $header = "From:" . $email . "\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }
    //Check if simple anti-bot test is correct
    if ($human !== 5) {
        $errHuman = 'Your anti-spam is incorrect';
    }
// If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
        if (mail($to, $subject, $body, $header)) {
            $result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
        <meta name="author" content="BootstrapBay.com">
        <title>Bootstrap Contact Form With PHP Example</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    <h1 class="page-header text-center">Contact Form Example</h1>
                    <form class="form-horizontal" role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                        <div class="form-group">
                            <label for="name" class="col-sm-2 control-label">Name</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
                                <?php if (isset($errName)) echo "<p class='text-danger'>$errName</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="email" class="col-sm-2 control-label">Email</label>
                            <div class="col-sm-10">
                                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">                                
                                <?php if (isset($errEmail)) echo "<p class='text-danger'>$errEmail</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="message" class="col-sm-2 control-label">Message</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" rows="4" name="message"></textarea>
                                <?php if (isset($errMessage)) echo "<p class='text-danger'>$errMessage</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
                                <?php if (isset($errHuman)) echo "<p class='text-danger'>$errHuman</p>" ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-10 col-sm-offset-2">
                                <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-10 col-sm-offset-2">                               
                                <?php if (isset($result)) echo $result ?>
                            </div>
                        </div>
                    </form> 
                </div>
            </div>
        </div>   
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    </body>
</html>

PHP isset() will determine if the variable is set and is not NULL. Initially the variables are NULL so it will omit showing you the errors.

Note: most of the time email will be delivered to your email address. check spam/junk folders to see the email. This happens due to unverified source of the email server.

Edit 1

Combining PHPMailer with mailgun platform will ensure you will get your emails to the destination address and possibly avoid going to spam/junk email folder.

Edit 2

I've update my answer with the recent modifications. Please have a look. Tested on a live server. received the contact demo email to my spam/junk folder on Outlook, Straightly to my inbox on Gmail.

Screens Form submit Outlook Gmail

Hope you will have good luck!

CodeMonkey
  • 2,828
  • 1
  • 23
  • 32
  • Thank you for your comment, the errors are gone locally :) But still don't receive the mail, also not in the spam/junk folder. I will have a look at it. Thank you! – Johan Feb 23 '17 at 12:47
  • @Johan, please share with us your server provider. I will post an update as soon as I get home. – CodeMonkey Feb 23 '17 at 12:51
  • My server provider is Versio. www.versio.nl – Johan Feb 23 '17 at 13:11
  • @Johan please refer to my whole answer. Script works fine. – CodeMonkey Feb 23 '17 at 19:04
  • Hmm okay. I will look at it. – Johan Feb 23 '17 at 22:47
  • Mark the correct answer and close this question please. :) – CodeMonkey Feb 24 '17 at 01:37
  • Did you change something at the code? Or is it the same in the begin post and edited with your script in this comment? – Johan Feb 24 '17 at 09:28
  • @Johan might have done slight changes to my first post. Use this because i tested this on a production server. Up vote the post to see this answer was helpful :) – CodeMonkey Feb 24 '17 at 09:33
  • I appreciate your help :) I receive emails from Mailgun, do I need to do something too with the PHPMailer to receive emails from the Bootstrap form? What I only change from your code is the email-address after $to. Is that correct? – Johan Feb 24 '17 at 10:21
  • @Johan you have to download the `PHPMailer` files and give `Mailgun`'s smtp details to the `PHPMailer` configuration file. That way you can see if your emails are properly sending or not. Otherwise this solution will works too. Only thing is you have to keep an eye on spam/junk folder. – CodeMonkey Feb 24 '17 at 10:32
  • replace `$to = "receivers@email.address";` with your email address. This will send the form contents to you. – CodeMonkey Feb 24 '17 at 10:33
  • It works :) Whatever I've tried it didn't work at my hosting :( My hosting doesn't support this because of the spam etc.. A friend of me have a website via an another hosting. So I've uploaded it to his website and it works perfectly :) Thanks for helping @Inuka – Johan Feb 24 '17 at 13:51
0

if you want the code ready without change it you only need to change your php.ini file and change the line relate to the error reporting. search for something like error_reporting = E_ALL and change to error_reporting = E_ALL & ~E_NOTICE with this you hide the undefine issue

and then you need to configure your smtp provider in your php.ini too

Yordankis
  • 93
  • 7