{"id":53,"date":"2010-09-08T14:21:43","date_gmt":"2010-09-08T05:21:43","guid":{"rendered":"http:\/\/santoshkori.com\/blog\/?p=53"},"modified":"2012-03-05T22:26:18","modified_gmt":"2012-03-05T13:26:18","slug":"how-to-avoid-spam-filters-with-php-mail-emails","status":"publish","type":"post","link":"https:\/\/www.santoshkori.com\/blog\/how-to-avoid-spam-filters-with-php-mail-emails\/","title":{"rendered":"How to Avoid Spam Filters with PHP mail() Emails"},"content":{"rendered":"<p>Just about everyone who uses <acronym title=\"Pre-Hypertext Processing\">PHP<\/acronym> has encountered the popular <acronym title=\"Pre-Hypertext Processing\">PHP<\/acronym> <strong>mail()<\/strong> function which enables email to be sent from a server. This function is preferred to other methods of sending email, such as <strong>sending mail with <acronym title=\"Simple Mail Transfer Protocol\">SMTP<\/acronym> Authentication<\/strong>, because its implementation is quick and easy. Unfortunately, when using the <strong>mail()<\/strong> function, your emails are more likely to be marked as spam. So how can we fix this?<br \/>\n<!--more--><\/p>\n<h2>A Simple Implementation Example<\/h2>\n<p>Many users of the <strong>mail()<\/strong> function often have simple implementations as shown in the code sample below:<\/p>\n<div>\n<p><code>&lt;?<br \/>\nmail(\"recipient@recipient.com\",\u00a0\"Message\",\u00a0\"A simple message.\", \"From: The Sender &lt;sender@sender.com&gt;\");<br \/>\n?&gt;<\/code><\/p>\n<\/div>\n<p>While this implementation will successfully send an email, the email will probably get caught in the recipient\u2019s spam filter. Fortunately, there are some simple fixes that can help you avoid spam filters.<\/p>\n<h2>4 Ways To Make Your <acronym title=\"Pre-Hypertext Processing\">PHP<\/acronym> mail() Emails Less Spammy<\/h2>\n<h3>1. Use Headers<\/h3>\n<p>In the simple example above, the from name and email address was added as the fourth parameter. Instead, <strong>consider using headers to set your <em>From<\/em> and <em>Reply-To<\/em> email addresses<\/strong>.<\/p>\n<p><code>&lt;?<br \/>\n$headers .= \"Reply-To: The Sender &lt;sender@sender.com&gt;\\r\\n\";<br \/>\n$headers .= \"Return-Path: The Sender &lt;sender@sender.com&gt;\\r\\n\";<br \/>\n$headers .= \"From: The Sender &lt;senter@sender.com&gt;\\r\\n\";<br \/>\n?&gt;<\/code><\/p>\n<p>But headers\u00a0 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.<\/p>\n<p><code>&lt;?<br \/>\n$headers .= \"Organization: Sender Organization\\r\\n\";<br \/>\n$headers .= \"<acronym title=\"Multipurpose Internet Mail Extension\">MIME<\/acronym>-Version: 1.0\\r\\n\";<br \/>\n$headers .= \"Content-type: text\/plain; charset=iso-8859-1\\r\\n\";<br \/>\n$headers .= \"X-Priority: 3\\r\\n\";<br \/>\n$headers .= \"X-Mailer: <acronym title=\"Pre-Hypertext Processing\">PHP<\/acronym>\". phpversion() .\"\\r\\n\"<br \/>\n?&gt;<\/code><\/p>\n<p>Be sure to replace the fourth parameter with the <strong>$headers<\/strong> variable as shown below.<\/p>\n<p><code>&lt;?<br \/>\nmail(\"recipient@recipient.com\",\u00a0\"Message\",\u00a0\"A simple message.\", $headers);<br \/>\n?&gt;<\/code><\/p>\n<h3>2. The Message Sender Domain and Server Domain Should Match<\/h3>\n<p>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 <em>example@example.com<\/em>, it is a good idea the the script reside on <em>example.com<\/em>.<\/p>\n<h3>3. Be Sure to Properly Use the Content-type Attribute<\/h3>\n<p>The <em>Content-type attribute<\/em> enables a message sender to say whether or not an email is plain text\u00a0 or html, or whether it has attachments. Obviously, the easiest to use content type is <em>text\/plain<\/em>. 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 <em>text\/html<\/em> content type, an html body tag is expected. Not having this tag could result in your email being marked as spam.<\/p>\n<h3>4. Verify That Your Server Is Not Blacklisted<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Alright, now that you have the basics on avoiding spam filters, reconstruct your scripts and happy emailing!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,17,15,14,5],"tags":[38,40,41,37,39],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-just-blogging","category-tips-n-tricks","category-tools","category-web-development","category-wordpress","tag-avoid-spam","tag-how-to-avoid-spam","tag-how-to-avoid-spam-mails","tag-php-mail","tag-spam-filters"],"_links":{"self":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":5,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":461,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions\/461"}],"wp:attachment":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}