How to Avoid Spam Filters with PHP mail() Emails

Just about everyone who uses PHP has encountered the popular PHP mail() function which enables email to be sent from a server. This function is preferred to other methods of sending email, such as sending mail with SMTP Authentication, because its implementation is quick and easy. Unfortunately, when using the mail() function, your emails are more likely to be marked as spam. So how can we fix this?

A Simple Implementation Example

Many users of the mail() function often have simple implementations as shown in the code sample below:

<?
mail("recipient@recipient.com", "Message", "A simple message.", "From: The Sender <sender@sender.com>");
?>

While this implementation will successfully send an email, the email will probably get caught in the recipient’s spam filter. Fortunately, there are some simple fixes that can help you avoid spam filters.

4 Ways To Make Your PHP mail() Emails Less Spammy

1. Use Headers

In the simple example above, the from name and email address was added as the fourth parameter. Instead, consider using headers to set your From and Reply-To email addresses.

<?
  $headers .= "Reply-To: The Sender <sender@sender.com>\r\n";
  $headers .= "Return-Path: The Sender <sender@sender.com>\r\n";
  $headers .= "From: The Sender <senter@sender.com>\r\n";
?>

But headers  are good for more than just setting details about the sender. They are also important for setting the content type, the email priority, and more. Here are how some additional headers look.

<?
  $headers .= "Organization: Sender Organization\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
  $headers .= "X-Priority: 3\r\n";
  $headers .= "X-Mailer: PHP". phpversion() ."\r\n"
?>

Be sure to replace the fourth parameter with the $headers variable as shown below.

<?
mail("recipient@recipient.com", "Message", "A simple message.", $headers);
?>

2. The Message Sender Domain and Server Domain Should Match

Spammers are notorious for sending emails from one server and trying to make the recipient believe that it came from somewhere else. So if you are sending an email from example@example.com, it is a good idea the the script reside on example.com.

3. Be Sure to Properly Use the Content-type Attribute

The Content-type attribute enables a message sender to say whether or not an email is plain text  or html, or whether it has attachments. Obviously, the easiest to use content type is text/plain. You just add your text as shown in the simple example, and you are done. But when you use the other content types, additional pieces might be expected. For example, with the text/html content type, an html body tag is expected. Not having this tag could result in your email being marked as spam.

4. Verify That Your Server Is Not Blacklisted

When a server is blacklisted, it means that that server has identified as one that has been sending a lot of spam. This results in recipient mail servers rejecting or filtering any mail that is received from that server.

So if your mail is not being received it is a good idea to verify that your server has not been blacklisted. This goes for both shared and dedicated servers. In a shared environment, it is common for other users on the server to be sending out spam. And in a dedicated environment, spammers may have found a way to exploit a vulnerability in a server or contact form to send out spam. So it is easy for either type of server to be blacklisted.

 

Alright, now that you have the basics on avoiding spam filters, reconstruct your scripts and happy emailing!

Tags: , ,

8 Responses to “How to Avoid Spam Filters with PHP mail() Emails”

  • Dennis Edell @ Direct Sales Marketing August 8, 2010 at 1:08 pm

    Are you back after gone a year?

    Has this blog been sold?

    What’s up?

    • Velvet Blues August 8, 2010 at 7:44 pm

      Yes, I am back and will be blogging. But future entries won’t be as frequent as they used to be. As you know, its quite hard to maintain a regular posting schedule.

      Thanks for visiting. :-)

      • Dennis Edell @ Direct Sales Marketing August 9, 2010 at 11:01 am

        You’ve been missed. May I ask what happened? You OK personally?

        • Velvet Blues August 10, 2010 at 6:16 pm

          Thanks for the concern. :-) All is well. The blog just took a back seat to more important development work.

          I do enjoy blogging, so now I am trying to shake the dust off and dive back in!

  • lafreebee August 17, 2010 at 9:20 pm

    I am going to try your email tips.
    Glad to hear your back your plugin fixed a lot of problems during a domain name change. Cheers David

  • Gary McCormick January 23, 2011 at 10:41 pm

    This info is great. I’ve had problems before with php email and spam, so glad to find this teaching. Will be checking out your other posts :-)

  • Sebastian @ Dinosaur Games February 5, 2011 at 12:52 pm

    Note: If you are a spammer you are forbidden to use this knowledge for your evil doings!

    Jk :)

    How could I find out if my server has been blacklisted? I’m using a shared server on a big host and could well imagine some bad people having misused it, if the mail() function should be available, which I guess it is.

    Sebastian

  • sanjib December 22, 2011 at 1:54 am

    When i send mail it goes to spam . so i avoid this problem?

    I am also putting headers, servr domain name is matched, Content-type Attribute & my server is not black listed

    So please give me solution

Trackbacks

Trackback URL:

Leave a Reply

Want us to work on your project?

Contact us today for a quote. Click here to submit details regarding your project.

If you are making a general inquiry, send an email to info@velvetblues.com

Go Daddy Deal of the Week: Cheap .COM Domains! Offer expires soon!