0

I was working on a message form that sends a copy of the message to the email address when it is submitted. It worked fine when the form action directed to a different php file. But when I directed the form to submit itself to the same file the form is in it sent the message to the database but didn't send the email. It would be great if someone could tell me why this is happening. Here's the code for contact.phpfile:

        <div class="form-wrap">
            <form class="form-flex" action="contact.php" method="post">
                <label for="">Send us a message</label>
                <input type="text" name="name" type="text" placeholder="Name">
                <input type="text" name="email" type="text" placeholder="Email">
                <textarea name="message" id="message" placeholder="Message"></textarea>
                <button class="btn-big" name="submit" type="submit">Send</button>
                <?php
                    if(isset($_POST['submit'])) {
                        $name = $_POST['name'];
                        $email = $_POST['email'];
                        $message = $_POST['message'];

                        $connection = mysqli_connect('host', 'user', 'password', 'messages');

                        $name = mysqli_real_escape_string($connection, $name);
                        $email = mysqli_real_escape_string($connection, $email);
                        $message = mysqli_real_escape_string($connection, $message);

                        if($connection) {
                            $query = "INSERT INTO msg(name,email,message) ";
                            $query .= "VALUES ('$name','$email','$message')";

                            mysqli_query($connection, $query);

                            $email_subject = 'New Message';
                            $email_body = $message;
                            $to = 'myemail@gmail.com';
                            mail($to, $email_subject, $email_body);

                            echo "<p class='form'>Message sent!</p>";
                        } else {
                            die("<p class='form'>Database connection failed</p>");
                        }                   
                    } 
                ?>
            </form>
        </div>
craft9861
  • 204
  • 2
  • 8
  • wrap `mail($to, $email_subject, $email_body)` in a try/catch block and see if you get an exception – Yash Karanke Jan 08 '20 at 11:36
  • If you get Db row inserted, then it means something wrong with `mail` function. I suggest using some lib like PHPMailer instead of `mail` function. Make sure you set proper headers for email to be successfully sent. Sometimes client may reject your simple `mail()` emails – Justinas Jan 08 '20 at 11:42
  • Yash Karanke, the try/catch block didn't return any exception but still I'm not getting any email. – craft9861 Jan 08 '20 at 11:48
  • Justinas, I tried it using the headers also but it didn't work. And yeah I think the problem is the mail function. But why was it working when the form was being submitted to a different php file? – craft9861 Jan 08 '20 at 11:51
  • you're missing the **headers** for the mail function. – Arun Karthick Jan 08 '20 at 12:37
  • So here's a crazy thing that solved the issue. I changed ````email_subject```` from 'New Message' to 'New Form Submission' (which I changed before and somehow this was causing the problem) and now the email is being sent. I've never been more baffled by a bug. – craft9861 Jan 08 '20 at 12:39

0 Answers0