Search code examples
phpemailspamspam-preventionemail-spam

PHP Mail is Being Sent to Spam


I know that there are many similar questions on this site, but none of them helped me.

I have the following PHP code:

<?php
$to = "[email protected]";
$from = "[email protected]";
$subject = "Confirm your registration!";
$message = "Please follow this link to confirm your registration: www.bit.ly/32106";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: HeyGee! <[email protected]>" . "\r\n" .
"Reply-To: [email protected]" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

$ok = @mail($to, $subject, $message, $headers, "-f " . $from);   
?>

Let's say [email protected] is my test e-mail. Whenever I send the mail to my address, I always receive the mail in my Spam folder. Why is that? Are there any corrections or tricks to be made to prevent the mail from arriving in Spam?

Thanks.


Solution

  • The reason why your mail is being sent to Spam folder is either because of the content of your email or that the receiving side is not able to verify if the email actually came from the stated domain in the from address, i.e., if the sender (you) are authorized to send email on behalf of heygee.com.

    Content part is easy to correct. You need to avoid bad grammar, ambiguous links (e.g links which say google.com but point to example.com), etc. Your message should be well worded (exclude those words frequently found in spam mails), and preferably include an unsubscribe link as well (if sent to a mailing list).

    Now comes the second and difficult part. The domain that you are writing in your from address should be the same domain from which you are executing this mail script or should be authorized by this domain's TXT records to send mail on its behalf. The simplest way to go about this would be (provided you have DNS access to the sending domain name) to add a TXT SPF record permitting the IP of the server your script resides on to send mail on its behalf. Example SPF record:

    "v=spf1 ip6:1080::8:800:200C:417A/96 -all"
    

    The above record means Allow any IPv6 address between 1080::8:800:0000:0000 and 1080::8:800:FFFF:FFFF.

    Checkout: SPF (Wikipedia)

    Also, you may have a look here http://www.openspf.org/

    Now if you don't have DNS access, then simply change the domain name of the from address to the domain name of the server and it should fix it.

    Hope it helps.