Zend_Validate-EmailAddress.xml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.set.email_address">
  4. <title>EmailAddress</title>
  5. <para>
  6. <classname>Zend_Validate_EmailAddress</classname> allows you to validate an email address. The validator first splits
  7. the email address on local-part @ hostname and attempts to match these against known specifications for email
  8. addresses and hostnames.
  9. </para>
  10. <para>
  11. <emphasis role="strong">Basic usage</emphasis>
  12. </para>
  13. <para>
  14. A basic example of usage is below:
  15. <programlisting role="php"><![CDATA[
  16. $validator = new Zend_Validate_EmailAddress();
  17. if ($validator->isValid($email)) {
  18. // email appears to be valid
  19. } else {
  20. // email is invalid; print the reasons
  21. foreach ($validator->getMessages() as $message) {
  22. echo "$message\n";
  23. }
  24. }
  25. ]]></programlisting>
  26. This will match the email address <code>$email</code> and on failure populate
  27. <code>$validator->getMessages()</code> with useful error messages.
  28. </para>
  29. <para>
  30. <emphasis role="strong">Complex local parts</emphasis>
  31. </para>
  32. <para>
  33. <classname>Zend_Validate_EmailAddress</classname> will match any valid email address according to RFC2822. For example,
  34. valid emails include <code>bob@domain.com</code>, <code>bob+jones@domain.us</code>,
  35. <code>"bob@jones"@domain.com</code> and <code>"bob jones"@domain.com</code>
  36. </para>
  37. <para>
  38. Some obsolete email formats will not currently validate (e.g. carriage returns or a
  39. "\" character in an email address).
  40. </para>
  41. <para>
  42. <emphasis role="strong">Validating different types of hostnames</emphasis>
  43. </para>
  44. <para>
  45. The hostname part of an email address is validated against
  46. <link linkend="zend.validate.set.hostname"><classname>Zend_Validate_Hostname</classname></link>. By default
  47. only DNS hostnames of the form <code>domain.com</code> are accepted, though if you wish you
  48. can accept IP addresses and Local hostnames too.
  49. </para>
  50. <para>
  51. To do this you need to instantiate <classname>Zend_Validate_EmailAddress</classname> passing a parameter to indicate
  52. the type of hostnames you want to accept. More details are included in <classname>Zend_Validate_Hostname</classname>,
  53. though an example of how to accept both DNS and Local hostnames appears below:
  54. <programlisting role="php"><![CDATA[
  55. $validator = new Zend_Validate_EmailAddress(
  56. Zend_Validate_Hostname::ALLOW_DNS |
  57. Zend_Validate_Hostname::ALLOW_LOCAL);
  58. if ($validator->isValid($email)) {
  59. // email appears to be valid
  60. } else {
  61. // email is invalid; print the reasons
  62. foreach ($validator->getMessages() as $message) {
  63. echo "$message\n";
  64. }
  65. }
  66. ]]></programlisting>
  67. </para>
  68. <para>
  69. <emphasis role="strong">Checking if the hostname actually accepts email</emphasis>
  70. </para>
  71. <para>
  72. Just because an email address is in the correct format, it doesn't necessarily mean that
  73. email address actually exists. To help solve this problem, you can use MX validation to
  74. check whether an MX (email) entry exists in the DNS record for the email's hostname.
  75. This tells you that the hostname accepts email, but doesn't tell you the exact email
  76. address itself is valid.
  77. </para>
  78. <para>
  79. MX checking is not enabled by default and at this time is only supported by UNIX platforms.
  80. To enable MX checking you can pass a second parameter to the <classname>Zend_Validate_EmailAddress</classname>
  81. constructor.
  82. <programlisting role="php"><![CDATA[
  83. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS,
  84. true);
  85. ]]></programlisting>
  86. Alternatively you can either pass <code>true</code> or <code>false</code> to
  87. <code>$validator->setValidateMx()</code> to enable or disable MX validation.
  88. </para>
  89. <para>
  90. By enabling this setting network functions will be used to check for the presence of an
  91. MX record on the hostname of the email address you wish to validate. Please be aware
  92. this will likely slow your script down.
  93. </para>
  94. <para>
  95. <emphasis role="strong">Validating International Domains Names</emphasis>
  96. </para>
  97. <para>
  98. <classname>Zend_Validate_EmailAddress</classname> will also match international characters that exist in some domains.
  99. This is known as International Domain Name (IDN) support. This is enabled by default, though
  100. you can disable this by changing the setting via the internal <classname>Zend_Validate_Hostname</classname> object
  101. that exists within <classname>Zend_Validate_EmailAddress</classname>.
  102. <programlisting role="php"><![CDATA[
  103. $validator->hostnameValidator->setValidateIdn(false);
  104. ]]></programlisting>
  105. More information on the usage of <code>setValidateIdn()</code> appears in the
  106. <classname>Zend_Validate_Hostname</classname> documentation.
  107. </para>
  108. <para>
  109. Please note IDNs are only validated if you allow DNS hostnames to be validated.
  110. </para>
  111. <para>
  112. <emphasis role="strong">Validating Top Level Domains</emphasis>
  113. </para>
  114. <para>
  115. By default a hostname will be checked against a list of known TLDs. This is enabled by
  116. default, though you can disable this by changing the setting via the internal
  117. <classname>Zend_Validate_Hostname</classname> object that exists within <classname>Zend_Validate_EmailAddress</classname>.
  118. <programlisting role="php"><![CDATA[
  119. $validator->hostnameValidator->setValidateTld(false);
  120. ]]></programlisting>
  121. More information on the usage of <code>setValidateTld()</code> appears in the
  122. <classname>Zend_Validate_Hostname</classname> documentation.
  123. </para>
  124. <para>
  125. Please note TLDs are only validated if you allow DNS hostnames to be validated.
  126. </para>
  127. </sect2>
  128. <!--
  129. vim:se ts=4 sw=4 et:
  130. -->