0

I am having a problem with my contact form on my website. The website is currently live but the contact form doesn't want to send any emails whether there are errors or not.

Here is my code. Please could you tell me where I made a mistake? Any advice would be helpful.

<?php

if(isset($_POST['email'])) {
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "tabaccogiftshop@gmail.com";
    $email_subject = $_POST['last_name'];

    /*function died($error) {
        // your error code can go here
        echo "We are very sorry but there were errors whith your submit";
        echo "These errors appear bellow. <br> <br>";
        echo $error."<br> <br>";
        echo "Please go back and fix those errors";
        die();
    }*/
    // validation expected data exists
    if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['comments']) || !isset($_POST['captcha'])) {
        $error_message = "Sva polja se moraju unijeti!";
    }

    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email_from = $_POST['email'];
    $comments = $_POST['comments'];
    $captcha = $_POST['captcha'];
    $error_message = "";
    $success = "";

    $string_exp = "/^[A-Za-z .'-]+$/";


    if(!preg_match($string_exp, $first_name)) {
        $error_message = "Name should not hold numbers!";
    }

    if(!preg_match($string_exp, $last_name)) {
        $error_message = "Subject should not hold numbers!";
    }

    if(!preg_match("/5/", $captcha)) {
        $error_message = "Captcha is not valid,  please try again!";
    }

    if(strlen($comments) < 2 ) {
        $error_message = "Message you have entered is too short,  please try again!";
    }

    if(strlen($error_message) > 0) {
        echo '

                        <div class="alert alert-danger alert-dismissible">
                            <strong>Error! </strong>'.$error_message.'
                            <button class="close" data-dismiss="alert"> &times;</button>
                        </div>

                        ';

                        include_once('includes-en/form-en.php');
                        include_once('includes-en/footer-en.php');

                        die();
    }

    if(isset($_POST['submit']) && strlen($error_message) === 0) {
        $success = "We have recieved your message,  we will get in touch with you as soon as possible!";
    }

    if(strlen($success) > 0) {
         echo '

                        <div class="alert alert-success alert-dismissible">
                            <strong>Success! </strong>'.$success.'
                            <button class="close" data-dismiss="alert"> &times;</button>
                    </div>


                        ';
    }




    function clean_string($string) {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }

    $email_message = "Form details below. \n\n";

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Message: ".clean_string($comments)."\n";
    // Create email headers_list
    $headers = 'From: '.$email_from.'\r\n'.'Reply To: '.$email_from.'\r\n'.'X-Mailer: PHP/' .phpversion();

    @mail($email_to, $email_subject, $email_message, $headers);
    ?>
    <?php
}

    ?>
Synchro
  • 35,538
  • 15
  • 81
  • 104
Kristijan
  • 13
  • 3
  • 1
    '*whether there are errors or not at all*' - Well, this is quite an important part and can't be dismissed. Is your error reporting enabled? Have you checked your logs? Has your emails been working before? Have you contacted your host? – Script47 Jun 22 '18 at 20:38
  • Sorry I havent ment on code or error logs, but on user input error messages instead. I dont have any errors it just refreshes my site and echoes success message, but doesnt actually sends the email. – Kristijan Jun 22 '18 at 20:41
  • You've intentionally suppressed any display of errors from the `mail` function with the `@`. Maybe remove that and see if you get any indication of what's going wrong? – Greg Schmidt Jun 22 '18 at 20:43
  • You are suppressing errors using `@mail()`, and you are not checking the result of `mail()`. Have you looked in the mail_log? Is it sending the email and it's not being delivered? – Mr Glass Jun 22 '18 at 20:43
  • I have actually tired to do it on a "php noob" way, using print_r and echo I echoed $headers and $email_message and oh well it echoed it right. But the problem is.....it doesnt proceeds it to an email. – Kristijan Jun 22 '18 at 20:48
  • Look in your mail server logs. And don't tag your questions with tags that are not relevant. – Synchro Jun 22 '18 at 20:57
  • I use only relevant tags mr. . – Kristijan Jun 23 '18 at 07:41

0 Answers0