phpemailspamphpmailer

How do I prevent mails sent through PHP mail() from going to spam?


I am using PHP's mail() function to send emails (sendmail process is running). But all the mails are going to spam (in case of gmail). I have tried many tricks that I found on the net but none is working, please tell me about any sure-shot trick.


Solution

  • You must to add a needle headers:

    Sample code :

    $headers = "From: myplace@example.com\r\n";
    $headers .= "Reply-To: myplace2@example.com\r\n";
    $headers .= "Return-Path: myplace@example.com\r\n";
    $headers .= "CC: sombodyelse@example.com\r\n";
    $headers .= "BCC: hidden@example.com\r\n";
    
    if ( mail($to,$subject,$message,$headers) ) {
       echo "The email has been sent!";
       } else {
       echo "The email has failed!";
       }
    ?>