0

I created a form styled with bootstrap and followed a PHP tutorial on how to make the form functional (when info is filled an email to be send to a specified email) but it doesn't work.

I have read a lot of threads on Stackoverflow but couldn't find a solution.

I configured my php.ini file (I think correctly) and still the mail() function doesn't work.

Below is my code. I am an absolute beginner in PHP and I really appreciate your help!

<?php

                    function has_header_injection($str) {
                        return preg_match( "/[\r\n]/", $str );
                    }

                    if (isset($_POST['contact_submit'])){

                        $name  = trim($_POST['name']);
                        $email = trim($_POST['email']);
                        $msg   = $_POST['message'];

                        if (has_header_injection($name)||($email)) {
                            die;
                        }

                    if ( !$name || !$email || !$message ) {
                        echo '<h2 class="success">All fields are required</h2> <a href="contact.php" class="btn btn-custom btn-sm">Try again</a>';
                    }    

                    $to = "example@email.com"; (this has my true email)
                    $subject  = "$name sent you a message via example's contact form";
                    $message  = "Name: $name\r\n";
                    $message .= "Email: $email\r\n";
                    $message .= "Message:\r\n$msg";

                    $headers  = "MIME-Version: 1.0\r\n";
                    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
                    $headers .= "From: $name <$email>\r\n";    
                    $headers .= "X-Priority: 1\r\n";    
                    $headers .= "X-MSMail-Priority: High\r\n\r\n";

                    mail($to, $subject, $message, $headers);



                ?>

                <h2 class="success"> Thank you! Your project request is submitted. We will get back to you within 24 hours.</h2>
                <p><a href="index.php" class="btn btn-custom btn-sm">&laquo; Go to home page</a></p>

                <?php } else { ?>



                <form action="" class="contact-form" id="contact-form" method="post" role="form">
                    <div class="form-group">
                        <label class="sr-only" for="name">Name</label>
                        <input type="text" class="form-control" id="name" name="name" required="" placeholder="Name">
                    </div>
                    <div class="form-group">
                        <label class="sr-only" for="email">Email</label>
                        <input type="email" class="form-control" id="email" name="email" required="" placeholder="Email">
                    </div>
                    <div class="form-group">
                        <textarea class="form-control" id="message" name="message" placeholder="Enter a brief description of your project" required=""></textarea>
                    </div>
                    <button type="submit" class="btn btn-custom" name="contact_submit">Send</button>
                </form>

                <?php } ?>

1 Answers1

2

Are you working on localhost, if yes then email functionality is not going to work, put your code on server.

Sachin Gawai
  • 351
  • 1
  • 4
  • 15