0

This code what i am using to send mail but unfortunately mail goes to spam folder. I tried both gmail and hotmail but case is same . php code is:

<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    die();
} 

$to_Email       = "zinan09@gmail.com"; //Replace with recipient email address
$subject        = 'My email from Somebody out there...'; //Subject line for emails

//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]))
{
    die();
}

//Sanitize input data using PHP filter_var().
$user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
    header('HTTP/1.1 500 Name is too short or empty!');
    exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
    header('HTTP/1.1 500 Please enter a valid email!');
    exit();
}
if(strlen($user_Message)<5) //check emtpy message
{
    header('HTTP/1.1 500 Too short message! Please enter something.');
    exit();
}

//proceed with PHP email.
$headers = 'From: '.$user_Email.'';

@$sentMail = mail($to_Email, $subject, $user_Message .'  -'.$user_Name, $headers);

if(!$sentMail)
{
    header('HTTP/1.1 500 Could not send mail! Sorry..');
    exit();
}else{
    echo 'Hi '.$user_Name .', Thank you for your email! ';
    echo 'Your email has already arrived in my Inbox, all I need to do is Check it.';
}
}
?>

Is there any missing function which i need to add or modify to solve this problem

Zinan Nadeem
  • 197
  • 3
  • 14
  • 1
    Well for one, there's no way in hell it can be from the user's own email. This is likely tripping spam detectors. – Daedalus Oct 25 '14 at 08:38
  • 1
    A spamm function within a mail client looks for specific keywords within a mail, if these words are matched, it will be moved to the spamm folder. You may want to revise your content you are sending with the email. Also the sender is important. I would use something like info@ <-- email client from your website, or no-reply@ <-- email client from your website, instead of your own email – Dorvalla Oct 25 '14 at 08:38

2 Answers2

6

Try to set From header to actual server domain address (i.e. no-reply@mysite.com), and move user email to Reply-To header. That would calm anti-spam filters down.

$headers = 'From: no-reply@mysite.com' . "\r\n";
$headers .= 'Reply-To: '.$user_Email.'';
vearutop
  • 3,924
  • 24
  • 41
0

As I said in the comments, you might wanna look at your content.

Also, your email sender is the same as the email receiver. I would revise that, making it a static email you cant reply to.

Also your mail has 5 variables, not sure why, but if you use headers, you can tuck them in there.

example.

    $message = wordwrap($message, 70);  
    $to = "info@somewebsite.com";  // change this propperty for your own email
    $subject = "Een gebruiker heeft een vraag of opmerking";

    $body = 'yadidyadi'. $message ;
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: noreply@jellyfishwebdesign.nl"; // make this one static.. i made mine noreply

    if (mail ($to, $subject, $body, $headers)) {
       echo 'yay, mail send';
    }

edit

just to point out, and this is not related to your question, your first check, the if($_POST) is kinda not relevant, since you wanna check if a certain button is pressed. if you put different code in this file for different processes, this whole mail thing will be fired anyway, despite if you are editing a post, entering something in the database ... anything this file will be called on.

Dorvalla
  • 5,027
  • 4
  • 28
  • 45