Zend_Validate-EmailAddress.xml 6.0 KB

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