Zend_Validate-EmailAddress.xml 14 KB

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