0

I have a "Contact Me" form in my website, which sends all messages submitted through it to my personal Gmail account.

For some reason, Gmail automatically sends most of the messages to the spam folder, and tags it as Phishing.

I've tried to read about the issue, and to rewrite my code, but I couldn't find the solution for this.

Doesn't anybody knows why?

here is my updated PHP form code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">

        <!-- Always force latest IE rendering engine -->
        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <!-- Mobile Specific Meta -->
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!-- TITLE -->
        <title>Thank You :)</title>

        <!-- BOOTSTRAP CSS -->
        <link rel="stylesheet" href="../css/bootstrap/bootstrap.min.css">

        <!-- FONT-AWESOME CSS -->
        <link rel="stylesheet" href="../css/font-awesome/css/font-awesome.min.css">

        <!-- MAIN STYLE CSS -->
        <link rel="stylesheet" href="../css/style.css">

        <!-- RESPONSIVE CSS -->
        <link rel="stylesheet" href="../css/responsive.css">

    </head>
    <body class="contact-p">

    <?php

    $TO = 'myemail@gmail.com';

    //message the subject of the email
    $SUBJECT = 'You've received a new message';
    $MSG_SEND_ERROR = 'Couldn't send the message. Please try again';

    // Sender Info
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = 'General subject';
    $message = $_POST['message'];
    $error = "";


    // Email regex
    $pattern = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$^";

    // test for name
    if (empty($name)) {
        $error .= 'error-name,'; // No name     
    }

    // test for email
    if (empty($email) || !preg_match_all($pattern, $email)) {
        $error .= 'error-email,'; // No Email   
    }

    // test for message
    if (empty($message)) {
        $error .= 'error-message'; // No Message    
    }


    //define the headers we want passed. Note that they are separated with \r\n
    $headers = "From: " . $name . " - " . $subject . " <" . $email . ">\r\nReply-To: " . $email . "";

    if (!$error) {

        //send the email
        $send = mail($TO, $SUBJECT, $message, $headers);

        if ($send) {
        ?>
        <div class="contact-f contact-success col-md-6">

            <i class="fa fa-smile-o fa-4x"></i>

            <h3>Message sent :)</h3>

        </div>
        <?php
        } else {
            // If the message is not send return error
        ?>
        <div class="contact-f contact-nosuccess col-md-6">

            <i class="fa fa-frown-o fa-4x"></i>

            <h3>Please fill the form</h3>

        </div>
    <?php
        }
    } else {
    ?>
    <div class="contact-f contact-nosuccess col-md-6">

        <i class="fa fa-frown-o fa-4x"></i>

        <h3>Please fill the form and try again</h3>

    </div>
    <?php
    }
    ?>

        <!-- ====== JS ======  -->
        <!-- JQUERY -->
        <script type="text/javascript" src="../js/jquery.v1.12.4.js"></script>
        <!-- BOOTSTRAP JS -->
        <script src="../js/bootstrap.min.js"></script>
        <!-- custom js -->
        <script src="../js/main.js"></script>
    </body>
</html>
F.SO7
  • 695
  • 1
  • 13
  • 37
  • Maybe the server you're on has been blacklisted, hard to say really. Could be anything. – Funk Forty Niner Mar 23 '20 at 14:30
  • 1
    This is a complex issue. Your PHP code is only one of the many places where things could go wrong. Here's a good general guide to debugging email problems in PHP: https://stackoverflow.com/a/24644450/5947043 – ADyson Mar 23 '20 at 14:30
  • 1
    Don’t use PHP’s `mail` function, use a proper mailer library (like PHPMailer or Swift Mailer), and send your mails via SMTP. – CBroe Mar 23 '20 at 14:31
  • If that is your *real* code; it contains parse errors. – Funk Forty Niner Mar 23 '20 at 14:37
  • @FunkFortyNiner What error? I am new to `PHP` – F.SO7 Mar 23 '20 at 14:38
  • In these 2 lines `$SUBJECT = 'You've received a new message'; $MSG_SEND_ERROR = 'Couldn't send the message. Please try again';`. The single quotes inside need to be escaped. Either that or encapsulate them in double quotes. – Funk Forty Niner Mar 23 '20 at 14:41
  • @FunkFortyNiner I've changed that to double quotes and it didn't solve the problem – F.SO7 Mar 23 '20 at 14:47

0 Answers0