{"id":39,"date":"2012-11-26T16:26:37","date_gmt":"2012-11-26T16:26:37","guid":{"rendered":"http:\/\/news.innerfire.net\/?p=39"},"modified":"2018-07-25T15:30:06","modified_gmt":"2018-07-25T15:30:06","slug":"php-email-validator","status":"publish","type":"post","link":"https:\/\/news.innerfire.net\/?p=39","title":{"rendered":"php email validator"},"content":{"rendered":"<p>If you are like me you get tired of constant email bounces from your web apps so I sat down and wrote this simple validator to save the mail server the extra load of having to deal with obvious typos.<\/p>\n<pre class=\"brush: php; gutter: true\">\/\/ email Validator\r\n\/\/ returns 1 on success \r\n\/\/ return -1 if the email is not in the correct format \r\n\/\/ return -2 if the email doesn't resolve \r\nfunction check_email($email) {   \r\n        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {\r\n                return (-1) ;\r\n        }\r\n\r\n        list($username, $domain) = explode('@', $email);\r\n\r\n        \/\/as per the RFC: check the domain for an MX record or A record and allow for IPv6 addresses\r\n        if (!checkdnsrr($domain, 'MX') &amp;&amp; !checkdnsrr($domain, 'A') &amp;&amp; !checkdnsrr($domain, 'AAAA')) {\r\n                return (-2) ;\r\n        } \r\n\r\n        return (1) ;\r\n}<\/pre>\n<pre>Here is a quick example of how to call it:<\/pre>\n<pre class=\"brush: actionscript3; gutter: true\">$ret = check_email($h_email) ;\r\n\r\nif ($ret &gt; 0) {\r\n        echo \"valid email&lt;br&gt;\" ;\r\n} else {\r\n        echo \"error: \" ;\r\n        if($ret == -1 ){\r\n                echo \"invalid format&lt;br&gt;\" ;\r\n        }\r\n        if($ret == -2 ) {\r\n                echo \"does not resolve&lt;br&gt;\" ;\r\n        }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are like me you get tired of constant email bounces from your web apps so I sat down and wrote this simple validator to save the mail server the extra load of having to deal with obvious typos. \/\/ email Validator \/\/ returns 1 on success \/\/ return -1 if the email is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-scripts"],"_links":{"self":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":15,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}