Zend_Validate-EmailAddress.xml 14 KB

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