-1

Can someone help me figure out why the below PHP is causing the email to be sent to Gmail spam? I tried following other directions for setting proper headers, but I still run into problems with my emails going into the spam filter in Gmail.

Any help would be appreciated!

<?php 
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['submit']))
{
    $name = trim($_POST['txt_name']);
    $fromemail = trim($_POST['txt_email']);
    $inquiry = trim($_POST['txt_inquiry']);
    $message = trim($_POST['txt_msg']);

    //name can contain only alpha characters and space
    if (!preg_match("/^[a-zA-Z ]+$/",$name))
    {
        $error = true;
        $name_error = "Please enter a real name";
    }
    if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
    {
        $error = true;
        $fromemail_error = "Please enter a valid email address";
    }
    if(empty($inquiry))
    {
        $error = true;
        $inquiry_error = "Please enter your subject";
    }
    if(empty($message))
    {
        $error = true;
        $message_error = "Please enter your message";
    }
    if (!$error)
    {
        //send mail
        $toemail = "myemail@gmail.com";
        $subject = "inquiry from visitor " . $name;
        $body = "Here goes your Message Details: \n\n Name: $name \n From: $fromemail \n Inquiry: $inquiry \n Message: \n $message";
        $headers = "From: $fromemail\n";
        $headers .= "Reply-To: $fromemail";
        $headers .= "Return-Path: $fromemail";

        if (mail ($toemail, $inquiry, $body, $headers))
            $alertmsg  = '<div class="alert alert-success text-center">Message sent successfully.  We will get back to you shortly!</div>';
        else
            $alertmsg = '<div class="alert alert-danger text-center">There is error in sending mail.  Please try again later.</div>';
    }
}

?>

kesernio
  • 75
  • 1
  • 4
  • 9
  • Have u checked your $headers with a dd() or var_dump() or sth??? because this type of writing is somehow risky – Peyman.H Sep 18 '15 at 12:44

2 Answers2

1

It could be that your ip address of the server that you are sending the email with it used for other purposes that marked it as a spam address.

Most of the time when you add DKIM and SPF to your email server (if you use one) will solve this problem.

An essier solution would be to use an external mailing service so that you they can handle that for you

-1

I often made the experience, that mails that are directly getting sent by PHP are recognized as SPAM...

my approach is now, to always use a SMTP-Server for sending those emails...

this post could help you!

Community
  • 1
  • 1
FalcoB
  • 1,251
  • 10
  • 23