Zend_Validate-EmailAddress.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. <sect3 id="zend.validate.set.email_address.basic">
  11. <title>Basic usage</title>
  12. <para>
  13. A basic example of usage is below:
  14. </para>
  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. <para>
  27. This will match the email address <varname>$email</varname> and on failure populate
  28. <code>$validator->getMessages()</code> with useful error messages.
  29. </para>
  30. </sect3>
  31. <sect3 id="zend.validate.set.email_address.options">
  32. <title>Options for validating Email Addresses</title>
  33. <para>
  34. <classname>Zend_Validate_EmailAddress</classname> supports several options which can
  35. either be set at initiation, by giving an array with the related options, or
  36. afterwards, by using <methodname>setOptions()</methodname>. The following options are
  37. supported:
  38. </para>
  39. <itemizedlist>
  40. <listitem>
  41. <para>
  42. <emphasis>allow</emphasis>: Defines which type of domain names are accepted.
  43. This option is used in conjunction with the hostname option to set the
  44. hostname validator. For more informations about possible values of this
  45. option, look at <link linkend="zend.validate.set.hostname">Hostname</link>
  46. and possible <constant>ALLOW</constant>* constants. This option defaults to
  47. <constant>ALLOW_DNS</constant>.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <emphasis>hostname</emphasis>: Sets the hostname validator with which the
  53. domain part of the email address will be validated.
  54. </para>
  55. </listitem>
  56. <listitem>
  57. <para>
  58. <emphasis>mx</emphasis>: Defines if the MX records from the server should be
  59. detected. If this option is defined to <constant>TRUE</constant> then the MX
  60. records are used to verify if the server
  61. accepts emails. This option defaults to <constant>FALSE</constant>.
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. <emphasis>deep</emphasis>: Defines if the servers MX records should be verified
  67. by a deep check. When this option is set to <constant>TRUE</constant> then
  68. additionally to MX records also the A, A6 and <constant>AAAA</constant> records
  69. are used to verify if the server accepts emails. This option defaults to
  70. <constant>FALSE</constant>.
  71. </para>
  72. </listitem>
  73. <listitem>
  74. <para>
  75. <emphasis>domain</emphasis>: Defines if the domain part should be checked.
  76. When this option is set to <constant>FALSE</constant>, then only the local part
  77. of the email address will be checked. In this case the hostname validator will
  78. not be called. This option defaults to <constant>TRUE</constant>.
  79. </para>
  80. </listitem>
  81. </itemizedlist>
  82. <programlisting language="php"><![CDATA[
  83. $validator = new Zend_Validate_EmailAddress();
  84. $validator->setOptions(array('domain' => false));
  85. ]]></programlisting>
  86. </sect3>
  87. <sect3 id="zend.validate.set.email_address.complexlocal">
  88. <title>Complex local parts</title>
  89. <para>
  90. <classname>Zend_Validate_EmailAddress</classname> will match any valid email address
  91. according to RFC2822. For example, valid emails include <code>bob@domain.com</code>,
  92. <code>bob+jones@domain.us</code>, <code>"bob@jones"@domain.com</code> and
  93. <code>"bob jones"@domain.com</code>
  94. </para>
  95. <para>
  96. Some obsolete email formats will not currently validate (e.g. carriage returns or a
  97. "\" character in an email address).
  98. </para>
  99. </sect3>
  100. <sect3 id="zend.validate.set.email_address.purelocal">
  101. <title>Validating only the local part</title>
  102. <para>
  103. If you need <classname>Zend_Validate_EmailAddress</classname> to check only the local
  104. part of an email address, and want to disable validation of the hostname, you can
  105. set the <property>domain</property> option to <constant>FALSE</constant>. This forces
  106. <classname>Zend_Validate_EmailAddress</classname> not to validate the hostname part of
  107. the email address.
  108. </para>
  109. <programlisting language="php"><![CDATA[
  110. $validator = new Zend_Validate_EmailAddress();
  111. $validator->setOptions(array('domain' => FALSE));
  112. ]]></programlisting>
  113. </sect3>
  114. <sect3 id="zend.validate.set.email_address.hostnametype">
  115. <title>Validating different types of hostnames</title>
  116. <para>
  117. The hostname part of an email address is validated against <link
  118. linkend="zend.validate.set.hostname">
  119. <classname>Zend_Validate_Hostname</classname></link>. By default
  120. only DNS hostnames of the form <code>domain.com</code> are accepted, though if you wish
  121. you can accept IP addresses and Local hostnames too.
  122. </para>
  123. <para>
  124. To do this you need to instantiate <classname>Zend_Validate_EmailAddress</classname>
  125. passing a parameter to indicate the type of hostnames you want to accept. More details
  126. are included in <classname>Zend_Validate_Hostname</classname>, though an example of how
  127. to accept both DNS and Local hostnames appears below:
  128. </para>
  129. <programlisting language="php"><![CDATA[
  130. $validator = new Zend_Validate_EmailAddress(
  131. Zend_Validate_Hostname::ALLOW_DNS |
  132. Zend_Validate_Hostname::ALLOW_LOCAL);
  133. if ($validator->isValid($email)) {
  134. // email appears to be valid
  135. } else {
  136. // email is invalid; print the reasons
  137. foreach ($validator->getMessages() as $message) {
  138. echo "$message\n";
  139. }
  140. }
  141. ]]></programlisting>
  142. </sect3>
  143. <sect3 id="zend.validate.set.email_address.checkacceptance">
  144. <title>Checking if the hostname actually accepts email</title>
  145. <para>
  146. Just because an email address is in the correct format, it doesn't necessarily mean
  147. that email address actually exists. To help solve this problem, you can use MX
  148. validation to check whether an MX (email) entry exists in the DNS record for the
  149. email's hostname. This tells you that the hostname accepts email, but doesn't tell you
  150. the exact email address itself is valid.
  151. </para>
  152. <para>
  153. MX checking is not enabled by default. To enable MX checking you can pass a second
  154. parameter to the <classname>Zend_Validate_EmailAddress</classname> constructor.
  155. </para>
  156. <programlisting language="php"><![CDATA[
  157. $validator = new Zend_Validate_EmailAddress(
  158. array(
  159. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  160. 'mx' => true
  161. )
  162. );
  163. ]]></programlisting>
  164. <note>
  165. <title>MX Check under Windows</title>
  166. <para>
  167. Within Windows environments MX checking is only available when
  168. <acronym>PHP</acronym> 5.3 or above is used. Below <acronym>PHP</acronym> 5.3 MX
  169. checking will not be used even if it's activated within the options.
  170. </para>
  171. </note>
  172. <para>
  173. Alternatively you can either pass <constant>TRUE</constant> or
  174. <constant>FALSE</constant> to <code>$validator->setValidateMx()</code> to enable or
  175. disable MX validation.
  176. </para>
  177. <para>
  178. By enabling this setting network functions will be used to check for the presence of an
  179. MX record on the hostname of the email address you wish to validate. Please be aware
  180. this will likely slow your script down.
  181. </para>
  182. <para>
  183. Sometimes validation for MX records returns <constant>FALSE</constant>, even if emails
  184. are accepted. The reason behind this behaviour is, that servers can accept emails even
  185. if they do not provide a MX record. In this case they can provide A, A6 or
  186. <constant>AAAA</constant> records. To allow
  187. <classname>Zend_Validate_EmailAddress</classname> to check also for these other records,
  188. you need to set deep MX validation. This can be done at initiation by setting the
  189. <property>deep</property> option or by using <methodname>setOptions()</methodname>.
  190. </para>
  191. <programlisting language="php"><![CDATA[
  192. $validator = new Zend_Validate_EmailAddress(
  193. array(
  194. 'allow' => Zend_Validate_Hostname::ALLOW_DNS,
  195. 'mx' => true,
  196. 'deep' => true
  197. )
  198. );
  199. ]]></programlisting>
  200. <warning>
  201. <title>Performance warning</title>
  202. <para>
  203. You should be aware that enabling MX check will slow down you script because of the
  204. used network functions. Enabling deep check will slow down your script even more as
  205. it searches the given server for 3 additional types.
  206. </para>
  207. </warning>
  208. <note>
  209. <title>Disallowed IP addresses</title>
  210. <para>
  211. You should note that MX validation is only accepted for external servers. When deep
  212. MX validation is enabled, then local IP addresses like <command>192.168.*</command>
  213. or <command>169.254.*</command> are not accepted.
  214. </para>
  215. </note>
  216. </sect3>
  217. <sect3 id="zend.validate.set.email_address.validateidn">
  218. <title>Validating International Domains Names</title>
  219. <para>
  220. <classname>Zend_Validate_EmailAddress</classname> will also match international
  221. characters that exist in some domains. This is known as International Domain Name (IDN)
  222. support. This is enabled by default, though you can disable this by changing the
  223. setting via the internal <classname>Zend_Validate_Hostname</classname> object that
  224. exists within <classname>Zend_Validate_EmailAddress</classname>.
  225. </para>
  226. <programlisting language="php"><![CDATA[
  227. $validator->getHostnameValidator()->setValidateIdn(false);
  228. ]]></programlisting>
  229. <para>
  230. More information on the usage of <methodname>setValidateIdn()</methodname> appears in
  231. the <classname>Zend_Validate_Hostname</classname> documentation.
  232. </para>
  233. <para>
  234. Please note IDNs are only validated if you allow DNS hostnames to be validated.
  235. </para>
  236. </sect3>
  237. <sect3 id="zend.validate.set.email_address.validatetld">
  238. <title>Validating Top Level Domains</title>
  239. <para>
  240. By default a hostname will be checked against a list of known TLDs. This is enabled by
  241. default, though you can disable this by changing the setting via the internal
  242. <classname>Zend_Validate_Hostname</classname> object that exists within
  243. <classname>Zend_Validate_EmailAddress</classname>.
  244. </para>
  245. <programlisting language="php"><![CDATA[
  246. $validator->getHostnameValidator()->setValidateTld(false);
  247. ]]></programlisting>
  248. <para>
  249. More information on the usage of <methodname>setValidateTld()</methodname> appears in
  250. the <classname>Zend_Validate_Hostname</classname> documentation.
  251. </para>
  252. <para>
  253. Please note TLDs are only validated if you allow DNS hostnames to be validated.
  254. </para>
  255. </sect3>
  256. <sect3 id="zend.validate.set.email_address.setmessage">
  257. <title>Setting messages</title>
  258. <para>
  259. <classname>Zend_Validate_EmailAddress</classname> makes also use of
  260. <classname>Zend_Validate_Hostname</classname> to check the hostname part of a given
  261. email address. As with Zend Framework 1.10 you can simply set messages for
  262. <classname>Zend_Validate_Hostname</classname> from within
  263. <classname>Zend_Validate_EmailAddress</classname>.
  264. </para>
  265. <programlisting language="php"><![CDATA[
  266. $validator = new Zend_Validate_EmailAddress();
  267. $validator->setMessages(
  268. array(
  269. Zend_Validate_Hostname::UNKNOWN_TLD => 'I don't know the TLD you gave'
  270. )
  271. );
  272. ]]></programlisting>
  273. <para>
  274. Before Zend Framework 1.10 you had to attach the messages to your own
  275. <classname>Zend_Validate_Hostname</classname>, and then set this validator within
  276. <classname>Zend_Validate_EmailAddress</classname> to get your own messages returned.
  277. </para>
  278. </sect3>
  279. </sect2>
  280. <!--
  281. vim:se ts=4 sw=4 et:
  282. -->