Php implements RFC-compatible email address verification

Source: Internet
Author: User
Tags tld in domain
Php implements RFC-compatible email address verification

  1. /*

  2. Copyright 2009 Dominic Sayers
  3. Dominic_sayers@hotmail.com)
  4. Http://www.dominicsayers.com)

  5. This source file is subject to the Common Public Attribution License Version 1.0 (CPAL) license.

  6. The license terms are available through the world-wide-web at http://www.opensource.org/licenses/cpal_1.0
  7. */
  8. Function is_email ($ email, $ checkDNS = false ){
  9. // Check that $ email is a valid address
  10. // (Http://tools.ietf.org/html/rfc3696)
  11. // (Http://tools.ietf.org/html/rfc5322#section-3.4.1)
  12. // (Http://tools.ietf.org/html/rfc5321#section-4.1.3)
  13. // (Http://tools.ietf.org/html/rfc4291#section-2.2)
  14. // (Http://tools.ietf.org/html/rfc1123#section-2.1)
  15. // Contemporary email addresses consist of a "local part" separated from
  16. // A "domain part" (a fully-qualified domain name) by an at-sign ("@").
  17. // (Http://tools.ietf.org/html/rfc3696#section-3)
  18. $ Index = strrpos ($ email ,'@');

  19. If ($ index = false) return false; // No at-sign

  20. If ($ index = 0) return false; // No local part
  21. If ($ index> 64) return false; // Local part too long

  22. $ LocalPart = substr ($ email, 0, $ index );

  23. $ Domain = substr ($ email, $ index + 1 );
  24. $ DomainLength = strlen ($ domain );
  25. If ($ domainLength = 0) return false; // No domain part
  26. If ($ domainLength> 255) return false; // Domain part too long

  27. // Let's check the local part for RFC compliance...

  28. //
  29. // Period (".") may... appear, but may not be used to start or end
  30. // Local part, nor may two or more consecutive periods appear.
  31. // (Http://tools.ietf.org/html/rfc3696#section-3)
  32. If (preg_match ('/^ \. | \. \. | \. $/', $ localPart)> 0) return false; // Dots in wrong place

  33. // Any ASCII graphic (printing) character other than

  34. // At-sign ("@"), backslash, double quote, comma, or square brackets may
  35. // Appear without quoting. If any of that list of excluded characters
  36. // Are to appear, they must be quoted
  37. // (Http://tools.ietf.org/html/rfc3696#section-3)
  38. If (preg_match ('/^ "(? :.) * "$/', $ LocalPart)> 0 ){
  39. // Local part is a quoted string
  40. If (preg_match ('/(? :.) + [^ \] "(? :.) +/', $ LocalPart)> 0) return false; // Unescaped quote character inside quoted string
  41. } Else {
  42. If (preg_match ('/[@ \ [\] \ ",]/', $ localPart)> 0)
  43. // Check all excluded characters are escaped
  44. $ Stripped = preg_replace ('/\\\ [\ [\\] \\\ ",]/','', $ localPart );
  45. If (preg_match ('/[@ \ [\] \ ",]/', $ stripped)> 0) return false; // Unquoted excluded characters
  46. }

  47. // Now let's check the domain part...

  48. // The domain name can also be replaced by an IP address in square brackets

  49. // (Http://tools.ietf.org/html/rfc3696#section-3)
  50. // (Http://tools.ietf.org/html/rfc5321#section-4.1.3)
  51. // (Http://tools.ietf.org/html/rfc4291#section-2.2)
  52. If (preg_match ('/^ \ [(.) +] $/', $ domain) === 1 ){
  53. // It's an address-literal
  54. $ AddressLiteral = substr ($ domain, 1, $ domainLength-2 );
  55. $ MatchesIP = array ();
  56. // Extract IPv4 part from the end of the address-literal (if there is one)
  57. If (preg_match ('/\ B (? :(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \.) {3 }(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) $/', $ AddressLiteral, $ matchesIP)> 0 ){
  58. $ Index = strrpos ($ addressLiteral, $ matchesIP [0]);
  59. If ($ index = 0 ){
  60. // Nothing there handle T a valid IPv4 address, so...
  61. Return true;
  62. } Else {
  63. // Assume it's an attempt at a mixed address (IPv6 + IPv4)
  64. If ($ addressLiteral [$ index-1]! = ':') Return false; // Character preceding IPv4 address must be ':'
  65. If (substr ($ addressLiteral, 0, 5 )! = 'Ipv6: ') return false; // RFC5321 section 4.1.3

  66. $ IPv6 = substr ($ addressLiteral, 5, ($ index = 7 )? 2: $ index-6 );

  67. $ GroupMax = 6;
  68. }
  69. } Else {
  70. // It must be an attempt at pure IPv6
  71. If (substr ($ addressLiteral, 0, 5 )! = 'Ipv6: ') return false; // RFC5321 section 4.1.3
  72. $ IPv6 = substr ($ addressLiteral, 5 );
  73. $ GroupMax = 8;
  74. }

  75. $ GroupCount = preg_match_all ('/^ [0-9a-fA-F] {} |\\: [0-9a-fA-F] {} | (.)/', $ IPv6, $ matchesIP );

  76. $ Index = strpos ($ IPv6 ,'::');

  77. If ($ index = false ){

  78. // We need exactly the right number of groups
  79. If ($ groupCount! ==$ GroupMax) return false; // RFC5321 section 4.1.3
  80. } Else {
  81. If ($ index! = Strrpos ($ IPv6, ':') return false; // More than one '::'
  82. $ GroupMax = ($ index = 0 | $ index = (strlen ($ IPv6)-2 ))? $ GroupMax: $ groupMax-1;
  83. If ($ groupCount> $ groupMax) return false; // Too quota IPv6 groups in address
  84. }

  85. // Check for unmatched characters

  86. Array_multisort ($ matchesIP [1], SORT_DESC );
  87. If ($ matchesIP [1] [0]! = '') Return false; // Illegal characters in address

  88. // It's a valid IPv6 address, so...

  89. Return true;
  90. } Else {
  91. // It's a domain name...

  92. // The syntax of a legal Internet host name was specified in RFC-952

  93. // One aspect of host name syntax is hereby changed:
  94. // Restriction on the first character is relaxed to allow either
  95. // Letter or a digit.
  96. // (Http://tools.ietf.org/html/rfc1123#section-2.1)
  97. //
  98. // Nb rfc 1123 updates RFC 1035, but this is not currently apparent from reading RFC 1035.
  99. //
  100. // Most common applications, including email and the Web, will generally not permit... escaped strings
  101. // (Http://tools.ietf.org/html/rfc3696#section-2)
  102. //
  103. // Characters outside the set of alphabetic characters, digits, and hyphen must not appear in domain name
  104. // Labels for SMTP clients or servers
  105. // (Http://tools.ietf.org/html/rfc5321#section-4.1.2)
  106. //
  107. // RFC5321 precludes the use of a trailing dot in a domain name for SMTP purposes
  108. // (Http://tools.ietf.org/html/rfc5321#section-4.1.2)
  109. $ Matches = array ();
  110. $ GroupCount = preg_match_all ('/(? : [0-9a-zA-Z] [0-9a-zA-Z-] {0, 61} [0-9a-zA-Z] | [a-zA-Z]) (? : \. | $) | (.)/', $ Domain, $ matches );
  111. $ Level = count ($ matches [0]);

  112. If ($ level = 1) return false; // Mail host can't be a TLD

  113. $ TLD = $ matches [0] [$ level-1];

  114. If (substr ($ TLD, strlen ($ TLD)-1, 1) ==='. ') return false; // TLD can't end in a dot
  115. If (preg_match ('/^ [0-9] + $/', $ TLD)> 0) return false; // TLD can't be all-numeric

  116. // Check for unmatched characters

  117. Array_multisort ($ matches [1], SORT_DESC );
  118. If ($ matches [1] [0]! = '') Return false; // Illegal characters in domain, or label longer than 63 characters

  119. // Check DNS?

  120. If ($ checkDNS & function_exists ('checkdnsrr ')){
  121. If (! (Checkdnsrr ($ domain, 'A') | checkdnsrr ($ domain, 'MX '))){
  122. Return false; // Domain doesn' t actually exist
  123. }
  124. }

  125. // Eliminate all other factors, and the one which remains must be the truth.

  126. // (Sherlock Holmes, The Sign of Four)
  127. Return true;
  128. }
  129. }

  130. Function unitTest ($ email, $ reason = ''){

  131. $ Expected = ($ reason = '')? True: false;
  132. $ Valid = is_email ($ email );
  133. $ Not = ($ valid )? '': 'Not ';
  134. $ Unexpected = ($ valid! ==$ Expected )? 'This was unexpected!':'';
  135. $ Reason = ($ reason = '')? "": "Reason: $ reason ";
  136. Return "The address$ EmailIs $ not valid. $ unexpected $ reason
    \ N ";
  137. }

  138. // Email validator test cases (Dominic Sayers, January 2009)

  139. // Valid addresses
  140. Echo unitTest ('First. last@example.com ');
  141. Echo unitTest ('1970 @ example.com ');
  142. Echo unitTest ('"first last" @ example.com ');
  143. Echo unitTest ('"first \" last "@ example.com'); // Not totally sure whether this is valid or not
  144. Echo unitTest ('First \ @ last@example.com ');
  145. Echo unitTest ('"first @ last" @ example.com ');
  146. Echo unitTest ('First \ last@example.com '); // Note that \ is escaped even in single-quote strings, so this is testing "first \ last" @ example.com
  147. Echo unitTest ('first. last@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.
  148. Examples. x23456789.x23456789. x23456789.x23456789. x23456789.x23456789. x23456789.x23456789. x23456789.x2345 ');
  149. Echo unitTest ('First. last @ [12.34.56.78] ');
  150. Echo unitTest ('First. last @ [IPv6: 12.34.56.78] ');
  151. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 12.34.56.78] ');
  152. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666: 12.34.56.78] ');
  153. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666] ');
  154. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555] ');
  155. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666:] ');
  156. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666: 7777: 8888] ');
  157. Echo unitTest ('First. last@x23456789012345678901234567890123456789012345678901234567890123.example.com ');
  158. Echo unitTest ('First. last@1xample.com ');
  159. Echo unitTest ('First. last@123.example.com ');

  160. // Invalid addresses

  161. Echo unitTest ('First. la', "No @");
  162. Echo unitTest ('@ example.com', "No local part ");
  163. Echo unitTest ('1970 @ example.com ', "Local part more than 64 characters ");
  164. Echo unitTest ('. first.last@example.com', "Local part starts with a dot ");
  165. Echo unitTest ('First. last.@example.com ', "Local part ends with a dot ");
  166. Echo unitTest ('First .. ts ', "Local part has consecutive dots ");
  167. Echo unitTest ('"first" last "@ example.com'," Local part contains unescaped excluded characters ");
  168. Echo unitTest ('First \\\@ last@example.com ', "Local part contains unescaped excluded characters ");
  169. Echo unitTest ('First. last @ ', "No domain ");
  170. Echo unitTest ('first. last@x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.x23456789.
  171. Examples. Examples. x23456789.x23456789. x23456789.x23456789. x23456789.x23456789. x23456789.x23456', "Domain exceeds 255 chars ");
  172. Echo unitTest ('First. last @ [. 12.34.56.78] ', "Only char that can precede IPv4 address is ':'");
  173. Echo unitTest ('First. last @ [12.34.56.789] ', "Can't be interpreted as IPv4 so IPv6 tag is missing ");
  174. Echo unitTest ('First. last @ [: 12.34.56.78] ', "IPv6 tag is missing ");
  175. Echo unitTest ('First. last @ [Protocol 5: 12.34.56.78] ', "IPv6 tag is wrong ");
  176. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 12.34.56.78] ', "Too ipvipv6 groups (4 max )");
  177. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 12.34.56.78] ', "Not enough IPv6 groups ");
  178. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666: 7777: 12.34.56.78] ', "Too ipvipv6 groups (6 max )");
  179. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666] ', "Not enough IPv6 groups ");
  180. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666: 7777: 8888: 9999] ', "Too ipvipv6 groups (8 max )");
  181. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555] ', "Too comment': '(can be none or one )");
  182. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 3333: 4444: 5555: 6666] ', "Too worker IPv6 groups (6 max )");
  183. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 333x: 4444: 5555] ', "x is not valid in an IPv6 address ");
  184. Echo unitTest ('First. last @ [IPv6: 1111: 2222: 33333: 4444: 5555] ', "33333 is not a valid group in an IPv6 address ");
  185. Echo unitTest ('First. last@example.123 ', "TLD can't be all digits ");
  186. Echo unitTest ('First. last @ com ', "Mail host must be second-or lower level ");
  187. Echo unitTest ('First. last@-xample.com ', "Label can't begin with a hyphen ");
  188. Echo unitTest ('First. last@exampl-.com ', "Label can't end with a hyphen ");
  189. Echo unitTest ('First. last@x234567890123456789012345678901234567890123456789012345678901234.example.com ', "Label can't be longer than 63 ETS ");

  190. // Test cases from rfc00006 (February 2004, http://tools.ietf.org/html/rfc3696#section-3)

  191. Echo unitTest ('ABC \ @ def@example.com ');
  192. Echo unitTest ('Fred \ Bloggs@example.com ');
  193. Echo unitTest ('Joe. \\\ Blow@example.com ');
  194. Echo unitTest ('"Abc @ def" @ example.com ');
  195. Echo unitTest ('"Fred Bloggs" @ example.com ');
  196. Echo unitTest ('User + mailbox@example.com ');
  197. Echo unitTest ('customer/department = shipping@example.com ');
  198. Echo unitTest ('$ A12345@example.com ');
  199. Echo unitTest ('! Def! Xyz % abc@example.com ');
  200. Echo unitTest ('_ somename@example.com ');

  201. // Test cases from Doug Lovell (LinuxJournal, June 2007, http://www.linuxjournal.com/article/9585)

  202. Echo unitTest ("dclo@us.ibm.com ");
  203. Echo unitTest ("abc \ @ def@example.com ");
  204. Echo unitTest ("abc \\\\@ example.com ");
  205. Echo unitTest ("Fred \ Bloggs@example.com ");
  206. Echo unitTest ("Joe. \ Blow@example.com ");
  207. Echo unitTest ("\" Abc @ def \ "@ example.com ");
  208. Echo unitTest ("\" Fred Bloggs \ "@ example.com ");
  209. Echo unitTest ("customer/department = shipping@example.com ");
  210. Echo unitTest ("\ $ A12345@example.com ");
  211. Echo unitTest ("! Def! Xyz % abc@example.com ");
  212. Echo unitTest ("_ somename@example.com ");
  213. Echo unitTest ("user + mailbox@example.com ");
  214. Echo unitTest ("peter.piper@example.com ");
  215. Echo unitTest ("Doug \" Ace \ "\ Lovell@example.com ");
  216. Echo unitTest ("\" Doug \ "Ace \" L. \ "@ example.com ");
  217. Echo unitTest ("abc @ def@example.com", "Doug Lovell says this shocould fail ");
  218. Echo unitTest ("abc \\\\@ def@example.com", "Doug Lovell says this shoshould fail ");
  219. Echo unitTest ("abc \ @ example.com", "Doug Lovell says this shoshould fail ");
  220. Echo unitTest ("@ example.com", "Doug Lovell says this showould fail ");
  221. Echo unitTest ("doug @", "Doug Lovell says this shoshould fail ");
  222. Echo unitTest ("\" qu@example.com "," Doug Lovell says this shocould fail ");
  223. Echo unitTest ("ote \" @ example.com "," Doug Lovell says this shoshould fail ");
  224. Echo unitTest (". dot@example.com", "Doug Lovell says this shocould fail ");
  225. Echo unitTest ("dot.@example.com", "Doug Lovell says this shocould fail ");
  226. Echo unitTest ("two..dot@example.com", "Doug Lovell says this shocould fail ");
  227. Echo unitTest ("\" Doug \ "Ace \" L. \ "@ example.com", "Doug Lovell says this shoshould fail ");
  228. Echo unitTest ("Doug \" Ace \ "\ L \. @ example.com", "Doug Lovell says this shoshould fail ");
  229. Echo unitTest ("hello world@example.com", "Doug Lovell says this shocould fail ");
  230. Echo unitTest ("gatsby@f. SC .ot.t.f. I .tzg.era.l.d.", "Doug Lovell says this shocould fail ");
  231. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.