Zend_Mail_Read.xml 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.mail.read">
  4. <title>Reading Mail Messages</title>
  5. <para>
  6. <classname>Zend_Mail</classname> can read mail messages from several local or remote mail storages. All of them have the
  7. same basic API to count and fetch messages and some of them implement additional interfaces for not so common
  8. features. For a feature overview of the implemented storages see the following table.
  9. </para>
  10. <table id="zend.mail.read.table-1">
  11. <title>Mail Read Feature Overview</title>
  12. <tgroup cols="5">
  13. <thead>
  14. <row>
  15. <entry>Feature</entry>
  16. <entry>Mbox</entry>
  17. <entry>Maildir</entry>
  18. <entry>Pop3</entry>
  19. <entry>IMAP</entry>
  20. </row>
  21. </thead>
  22. <tbody>
  23. <row>
  24. <entry>Storage type</entry>
  25. <entry>local</entry>
  26. <entry>local</entry>
  27. <entry>remote</entry>
  28. <entry>remote</entry>
  29. </row>
  30. <row>
  31. <entry>Fetch message</entry>
  32. <entry>Yes</entry>
  33. <entry>Yes</entry>
  34. <entry>Yes</entry>
  35. <entry>Yes</entry>
  36. </row>
  37. <row>
  38. <entry>Fetch MIME-part</entry>
  39. <entry>emulated</entry>
  40. <entry>emulated</entry>
  41. <entry>emulated</entry>
  42. <entry>emulated</entry>
  43. </row>
  44. <row>
  45. <entry>Folders</entry>
  46. <entry>Yes </entry>
  47. <entry>Yes</entry>
  48. <entry>No</entry>
  49. <entry>Yes</entry>
  50. </row>
  51. <row>
  52. <entry>Create message/folder</entry>
  53. <entry>No</entry>
  54. <entry>todo</entry>
  55. <entry>No</entry>
  56. <entry>todo</entry>
  57. </row>
  58. <row>
  59. <entry>Flags</entry>
  60. <entry>No</entry>
  61. <entry>Yes</entry>
  62. <entry>No</entry>
  63. <entry>Yes</entry>
  64. </row>
  65. <row>
  66. <entry>Quota</entry>
  67. <entry>No</entry>
  68. <entry>Yes</entry>
  69. <entry>No</entry>
  70. <entry>No</entry>
  71. </row>
  72. </tbody>
  73. </tgroup>
  74. </table>
  75. <sect2 id="zend.mail.read-example">
  76. <title>Simple example using Pop3</title>
  77. <programlisting language="php"><![CDATA[
  78. $mail = new Zend_Mail_Storage_Pop3(array('host' => 'localhost',
  79. 'user' => 'test',
  80. 'password' => 'test'));
  81. echo $mail->countMessages() . " messages found\n";
  82. foreach ($mail as $message) {
  83. echo "Mail from '{$message->from}': {$message->subject}\n";
  84. }
  85. ]]></programlisting>
  86. </sect2>
  87. <sect2 id="zend.mail.read-open-local">
  88. <title>Opening a local storage</title>
  89. <para>
  90. Mbox and Maildir are the two supported formats for local mail storages, both in their most simple formats.
  91. </para>
  92. <para>
  93. If you want to read from a Mbox file you only need to give the filename to the constructor of
  94. <classname>Zend_Mail_Storage_Mbox</classname>:
  95. </para>
  96. <programlisting language="php"><![CDATA[
  97. $mail = new Zend_Mail_Storage_Mbox(array('filename' =>
  98. '/home/test/mail/inbox'));
  99. ]]></programlisting>
  100. <para>Maildir is very similar but needs a dirname:</para>
  101. <programlisting language="php"><![CDATA[
  102. $mail = new Zend_Mail_Storage_Maildir(array('dirname' =>
  103. '/home/test/mail/'));
  104. ]]></programlisting>
  105. <para>Both constructors throw a <classname>Zend_Mail_Exception</classname> if the storage can't be read.</para>
  106. </sect2>
  107. <sect2 id="zend.mail.read-open-remote">
  108. <title>Opening a remote storage</title>
  109. <para>
  110. For remote storages the two most popular protocols are supported: Pop3 and Imap. Both need at least a host
  111. and a user to connect and login. The default password is an empty string, the default port as given in the
  112. protocol RFC.
  113. </para>
  114. <programlisting language="php"><![CDATA[
  115. // connecting with Pop3
  116. $mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
  117. 'user' => 'test',
  118. 'password' => 'test'));
  119. // connecting with Imap
  120. $mail = new Zend_Mail_Storage_Imap(array('host' => 'example.com',
  121. 'user' => 'test',
  122. 'password' => 'test'));
  123. // example for a none standard port
  124. $mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
  125. 'port' => 1120
  126. 'user' => 'test',
  127. 'password' => 'test'));
  128. ]]></programlisting>
  129. <para>
  130. For both storages SSL and TLS are supported. If you use SSL the default port changes as given in the RFC.
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. // examples for Zend_Mail_Storage_Pop3, same works for Zend_Mail_Storage_Imap
  134. // use SSL on different port (default is 995 for Pop3 and 993 for Imap)
  135. $mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
  136. 'user' => 'test',
  137. 'password' => 'test',
  138. 'ssl' => 'SSL'));
  139. // use TLS
  140. $mail = new Zend_Mail_Storage_Pop3(array('host' => 'example.com',
  141. 'user' => 'test',
  142. 'password' => 'test',
  143. 'ssl' => 'TLS'));
  144. ]]></programlisting>
  145. <para>
  146. Both constructors can throw <classname>Zend_Mail_Exception</classname> or <classname>Zend_Mail_Protocol_Exception</classname>
  147. (extends <classname>Zend_Mail_Exception</classname>), depending on the type of error.
  148. </para>
  149. </sect2>
  150. <sect2 id="zend.mail.read-fetching">
  151. <title>Fetching messages and simple methods</title>
  152. <para>
  153. Messages can be fetched after you've opened the storage . You need the message number, which is a counter
  154. starting with 1 for the first message. To fetch the message, you use the method <code>getMessage()</code>:
  155. </para>
  156. <programlisting language="php"><![CDATA[
  157. $message = $mail->getMessage($messageNum);
  158. ]]></programlisting>
  159. <para>
  160. Array access is also supported, but this access method won't supported any additional parameters that could be added to
  161. <code>getMessage()</code>. As long as you don't mind, and can live with the default values, you may use:
  162. </para>
  163. <programlisting language="php"><![CDATA[
  164. $message = $mail[$messageNum];
  165. ]]></programlisting>
  166. <para>For iterating over all messages the Iterator interface is implemented:</para>
  167. <programlisting language="php"><![CDATA[
  168. foreach ($mail as $messageNum => $message) {
  169. // do stuff ...
  170. }
  171. ]]></programlisting>
  172. <para>
  173. To count the messages in the storage, you can either use the method <code>countMessages()</code> or use array
  174. access:
  175. </para>
  176. <programlisting language="php"><![CDATA[
  177. // method
  178. $maxMessage = $mail->countMessages();
  179. // array access
  180. $maxMessage = count($mail);
  181. ]]></programlisting>
  182. <para>To remove a mail, you use the method <code>removeMessage()</code> or again array access:</para>
  183. <programlisting language="php"><![CDATA[
  184. // method
  185. $mail->removeMessage($messageNum);
  186. // array access
  187. unset($mail[$messageNum]);
  188. ]]></programlisting>
  189. </sect2>
  190. <sect2 id="zend.mail.read-message">
  191. <title>Working with messages</title>
  192. <para>After you fetch the messages with <code>getMessage()</code> you want to fetch headers, the content
  193. or single parts of a multipart message. All headers can be accessed via properties or the method
  194. <code>getHeader()</code> if you want more control or have unusual header names. The header names are
  195. lower-cased internally, thus the case of the header name in the mail message doesn't matter. Also headers
  196. with a dash can be written in camel-case. If no header is found for both notations an exception is thrown.
  197. To encounter this the method <code>headerExists()</code> can be used to check the existance of a header.</para>
  198. <programlisting language="php"><![CDATA[
  199. // get the message object
  200. $message = $mail->getMessage(1);
  201. // output subject of message
  202. echo $message->subject . "\n";
  203. // get content-type header
  204. $type = $message->contentType;
  205. // check if CC isset:
  206. if( isset($message->cc) ) { // or $message->headerExists('cc');
  207. $cc = $message->cc;
  208. }
  209. ]]></programlisting>
  210. <para>If you have multiple headers with the same name- i.e. the Received headers- you might want an array
  211. instead of a string. In this case, use the <code>getHeader()</code> method.</para>
  212. <programlisting language="php"><![CDATA[
  213. // get header as property - the result is always a string,
  214. // with new lines between the single occurrences in the message
  215. $received = $message->received;
  216. // the same via getHeader() method
  217. $received = $message->getHeader('received', 'string');
  218. // better an array with a single entry for every occurrences
  219. $received = $message->getHeader('received', 'array');
  220. foreach ($received as $line) {
  221. // do stuff
  222. }
  223. // if you don't define a format you'll get the internal representation
  224. // (string for single headers, array for multiple)
  225. $received = $message->getHeader('received');
  226. if (is_string($received)) {
  227. // only one received header found in message
  228. }
  229. ]]></programlisting>
  230. <para>The method <code>getHeaders()</code> returns all headers as array with the lower-cased name as
  231. key and the value as and array for multiple headers or as string for single headers.</para>
  232. <programlisting language="php"><![CDATA[
  233. // dump all headers
  234. foreach ($message->getHeaders() as $name => $value) {
  235. if (is_string($value)) {
  236. echo "$name: $value\n";
  237. continue;
  238. }
  239. foreach ($value as $entry) {
  240. echo "$name: $entry\n";
  241. }
  242. }
  243. ]]></programlisting>
  244. <para>If you don't have a multipart message, fetching the content is easily done via
  245. <code>getContent()</code>. Unlike the headers, the content is only fetched when needed (aka late-fetch).</para>
  246. <programlisting language="php"><![CDATA[
  247. // output message content for HTML
  248. echo '<pre>';
  249. echo $message->getContent();
  250. echo '</pre>';
  251. ]]></programlisting>
  252. <para>Checking for multipart messages is done with the method <code>isMultipart()</code>. If you have
  253. multipart message you can get an instance of <classname>Zend_Mail_Part</classname> with the method
  254. <code>getPart()</code>. <classname>Zend_Mail_Part</classname> is the base class of <classname>Zend_Mail_Message</classname>,
  255. so you have the same methods: <code>getHeader()</code>, <code>getHeaders()</code>, <code>getContent()</code>,
  256. <code>getPart()</code>, <code>isMultipart</code> and the properties for headers.</para>
  257. <programlisting language="php"><![CDATA[
  258. // get the first none multipart part
  259. $part = $message;
  260. while ($part->isMultipart()) {
  261. $part = $message->getPart(1);
  262. }
  263. echo 'Type of this part is ' . strtok($part->contentType, ';') . "\n";
  264. echo "Content:\n";
  265. echo $part->getContent();
  266. ]]></programlisting>
  267. <para><classname>Zend_Mail_Part</classname> also implements <code>RecursiveIterator</code>, which makes it easy to scan through all parts. And
  268. for easy output, it also implements the magic method <code>__toString()</code>, which returns the content.</para>
  269. <programlisting language="php"><![CDATA[
  270. // output first text/plain part
  271. $foundPart = null;
  272. foreach (new RecursiveIteratorIterator($mail->getMessage(1)) as $part) {
  273. try {
  274. if (strtok($part->contentType, ';') == 'text/plain') {
  275. $foundPart = $part;
  276. break;
  277. }
  278. } catch (Zend_Mail_Exception $e) {
  279. // ignore
  280. }
  281. }
  282. if (!$foundPart) {
  283. echo 'no plain text part found';
  284. } else {
  285. echo "plain text part: \n" . $foundPart;
  286. }
  287. ]]></programlisting>
  288. </sect2>
  289. <sect2 id="zend.mail.read-flags">
  290. <title>Checking for flags</title>
  291. <para>Maildir and IMAP support storing flags. The class <classname>Zend_Mail_Storage</classname> has constants for all known
  292. maildir and IMAP system flags, named <classname>Zend_Mail_Storage::FLAG_&lt;flagname&gt;</classname>. To check
  293. for flags <classname>Zend_Mail_Message</classname> has a method called <code>hasFlag()</code>. With
  294. <code>getFlags()</code> you'll get all set flags.</para>
  295. <programlisting language="php"><![CDATA[
  296. // find unread messages
  297. echo "Unread mails:\n";
  298. foreach ($mail as $message) {
  299. if ($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) {
  300. continue;
  301. }
  302. // mark recent/new mails
  303. if ($message->hasFlag(Zend_Mail_Storage::FLAG_RECENT)) {
  304. echo '! ';
  305. } else {
  306. echo ' ';
  307. }
  308. echo $message->subject . "\n";
  309. }
  310. // check for known flags
  311. $flags = $message->getFlags();
  312. echo "Message is flagged as: ";
  313. foreach ($flags as $flag) {
  314. switch ($flag) {
  315. case Zend_Mail_Storage::FLAG_ANSWERED:
  316. echo 'Answered ';
  317. break;
  318. case Zend_Mail_Storage::FLAG_FLAGGED:
  319. echo 'Flagged ';
  320. break;
  321. // ...
  322. // check for other flags
  323. // ...
  324. default:
  325. echo $flag . '(unknown flag) ';
  326. }
  327. }
  328. ]]></programlisting>
  329. <para>As IMAP allows user or client defined flags, you could get flags that don't have a constant in
  330. <classname>Zend_Mail_Storage</classname>. Instead, they are returned as strings and can be checked the same way with
  331. <code>hasFlag()</code>.</para>
  332. <programlisting language="php"><![CDATA[
  333. // check message for client defined flags $IsSpam, $SpamTested
  334. if (!$message->hasFlag('$SpamTested')) {
  335. echo 'message has not been tested for spam';
  336. } else if ($message->hasFlag('$IsSpam')) {
  337. echo 'this message is spam';
  338. } else {
  339. echo 'this message is ham';
  340. }
  341. ]]></programlisting>
  342. </sect2>
  343. <sect2 id="zend.mail.read-folders">
  344. <title>Using folders</title>
  345. <para>
  346. All storages, except Pop3, support folders, also called mailboxes. The interface implemented by all storages
  347. supporting folders is called <classname>Zend_Mail_Storage_Folder_Interface</classname>. Also all of these classes have an
  348. additional optional parameter called <code>folder</code>, which is the folder selected after login, in the constructor.
  349. </para>
  350. <para>
  351. For the local storages you need to use separate classes called <classname>Zend_Mail_Storage_Folder_Mbox</classname> or
  352. <classname>Zend_Mail_Storage_Folder_Maildir</classname>. Both need one parameter called <code>dirname</code> with the name of the base dir.
  353. The format for maildir is as defined in maildir++ (with a dot as default delimiter), Mbox is a directory
  354. hierarchy with Mbox files. If you don't have a Mbox file called INBOX in your Mbox base dir you need to set
  355. another folder in the constructor.
  356. </para>
  357. <para>
  358. <classname>Zend_Mail_Storage_Imap</classname> already supports folders by default. Examples for opening these storages:
  359. </para>
  360. <programlisting language="php"><![CDATA[
  361. // mbox with folders
  362. $mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' =>
  363. '/home/test/mail/'));
  364. // mbox with a default folder not called INBOX, also works
  365. // with Zend_Mail_Storage_Folder_Maildir and Zend_Mail_Storage_Imap
  366. $mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' =>
  367. '/home/test/mail/',
  368. 'folder' =>
  369. 'Archive'));
  370. // maildir with folders
  371. $mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' =>
  372. '/home/test/mail/'));
  373. // maildir with colon as delimiter, as suggested in Maildir++
  374. $mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' =>
  375. '/home/test/mail/',
  376. 'delim' => ':'));
  377. // imap is the same with and without folders
  378. $mail = new Zend_Mail_Storage_Imap(array('host' => 'example.com',
  379. 'user' => 'test',
  380. 'password' => 'test'));
  381. ]]></programlisting>
  382. <para>
  383. With the method getFolders($root = null) you can get the folder hierarchy starting with the root folder or
  384. the given folder. It's returned as an instance of <classname>Zend_Mail_Storage_Folder</classname>, which implements
  385. <code>RecursiveIterator</code> and all children are also instances of <classname>Zend_Mail_Storage_Folder</classname>. Each of
  386. these instances has a local and a global name returned by the methods <code>getLocalName()</code> and
  387. <code>getGlobalName()</code>. The global name is the absolute name from the root folder (including
  388. delimiters), the local name is the name in the parent folder.
  389. </para>
  390. <table id="zend.mail.read-folders.table-1">
  391. <title>Mail Folder Names</title>
  392. <tgroup cols="2">
  393. <thead>
  394. <row>
  395. <entry>Global Name</entry>
  396. <entry>Local Name</entry>
  397. </row>
  398. </thead>
  399. <tbody>
  400. <row>
  401. <entry>/INBOX</entry>
  402. <entry>INBOX</entry>
  403. </row>
  404. <row>
  405. <entry>/Archive/2005</entry>
  406. <entry>2005</entry>
  407. </row>
  408. <row>
  409. <entry>List.ZF.General</entry>
  410. <entry>General</entry>
  411. </row>
  412. </tbody>
  413. </tgroup>
  414. </table>
  415. <para>
  416. If you use the iterator, the key of the current element is the local name. The global name is also returned
  417. by the magic method <code>__toString()</code>. Some folders may not be selectable, which means they can't
  418. store messages and selecting them results in an error. This can be checked with the method
  419. <code>isSelectable()</code>. So it's very easy to output the whole tree in a view:
  420. </para>
  421. <programlisting language="php"><![CDATA[
  422. $folders = new RecursiveIteratorIterator($this->mail->getFolders(),
  423. RecursiveIteratorIterator::SELF_FIRST);
  424. echo '<select name="folder">';
  425. foreach ($folders as $localName => $folder) {
  426. $localName = str_pad('', $folders->getDepth(), '-', STR_PAD_LEFT) .
  427. $localName;
  428. echo '<option';
  429. if (!$folder->isSelectable()) {
  430. echo ' disabled="disabled"';
  431. }
  432. echo ' value="' . htmlspecialchars($folder) . '">'
  433. . htmlspecialchars($localName) . '</option>';
  434. }
  435. echo '</select>';
  436. ]]></programlisting>
  437. <para>
  438. The current selected folder is returned by the method <code>getSelectedFolder()</code>. Changing the folder
  439. is done with the method <code>selectFolder()</code>, which needs the global name as parameter. If you want
  440. to avoid to write delimiters you can also use the properties of a <classname>Zend_Mail_Storage_Folder</classname> instance:
  441. </para>
  442. <programlisting language="php"><![CDATA[
  443. // depending on your mail storage and its settings $rootFolder->Archive->2005
  444. // is the same as:
  445. // /Archive/2005
  446. // Archive:2005
  447. // INBOX.Archive.2005
  448. // ...
  449. $folder = $mail->getFolders()->Archive->2005;
  450. echo 'Last folder was '
  451. . $mail->getSelectedFolder()
  452. . "new folder is $folder\n";
  453. $mail->selectFolder($folder);
  454. ]]></programlisting>
  455. </sect2>
  456. <sect2 id="zend.mail.read-advanced">
  457. <title>Advanced Use</title>
  458. <sect3 id="zend.mail.read-advanced.noop">
  459. <title>Using NOOP</title>
  460. <para>
  461. If you're using a remote storage and have some long tasks you might need to keep the connection alive
  462. via noop:
  463. </para>
  464. <programlisting language="php"><![CDATA[
  465. foreach ($mail as $message) {
  466. // do some calculations ...
  467. $mail->noop(); // keep alive
  468. // do something else ...
  469. $mail->noop(); // keep alive
  470. }
  471. ]]></programlisting>
  472. </sect3>
  473. <sect3 id="zend.mail.read-advanced.caching">
  474. <title>Caching instances</title>
  475. <para>
  476. <classname>Zend_Mail_Storage_Mbox</classname>, <classname>Zend_Mail_Storage_Folder_Mbox</classname>, <classname>Zend_Mail_Storage_Maildir</classname> and
  477. <classname>Zend_Mail_Storage_Folder_Maildir</classname> implement the magic methods <code>__sleep()</code> and
  478. <code>__wakeup()</code>, which means they are serializable. This avoids parsing the files or directory tree
  479. more than once. The disadvantage is that your Mbox or Maildir storage should not change. Some easy checks
  480. may be done, like reparsing the current Mbox file if the modification time changes, or reparsing the folder
  481. structure if a folder has vanished (which still results in an error, but you can search for another folder
  482. afterwards). It's better if you have something like a signal file for changes and check it before using the
  483. cached instance.
  484. </para>
  485. <programlisting language="php"><![CDATA[
  486. // there's no specific cache handler/class used here,
  487. // change the code to match your cache handler
  488. $signal_file = '/home/test/.mail.last_change';
  489. $mbox_basedir = '/home/test/mail/';
  490. $cache_id = 'example mail cache ' . $mbox_basedir . $signal_file;
  491. $cache = new Your_Cache_Class();
  492. if (!$cache->isCached($cache_id) ||
  493. filemtime($signal_file) > $cache->getMTime($cache_id)) {
  494. $mail = new Zend_Mail_Storage_Folder_Pop3(array('dirname' =>
  495. $mbox_basedir));
  496. } else {
  497. $mail = $cache->get($cache_id);
  498. }
  499. // do stuff ...
  500. $cache->set($cache_id, $mail);
  501. ]]></programlisting>
  502. </sect3>
  503. <sect3 id="zend.mail.read-advanced.extending">
  504. <title>Extending Protocol Classes</title>
  505. <para>
  506. Remote storages use two classes: <classname>Zend_Mail_Storage_&lt;Name&gt;</classname> and
  507. <classname>Zend_Mail_Protocol_&lt;Name&gt;</classname>. The protocol class translates the protocol commands and
  508. responses from and to PHP, like methods for the commands or variables with different structures for
  509. data. The other/main class implements the common interface.
  510. </para>
  511. <para>
  512. If you need additional protocol features, you can extend the protocol class and use it in the
  513. constructor of the main class. As an example, assume we need to knock different ports before we can
  514. connect to POP3.
  515. </para>
  516. <programlisting language="php"><![CDATA[
  517. class Example_Mail_Exception extends Zend_Mail_Exception
  518. {
  519. }
  520. class Example_Mail_Protocol_Exception extends Zend_Mail_Protocol_Exception
  521. {
  522. }
  523. class Example_Mail_Protocol_Pop3_Knock extends Zend_Mail_Protocol_Pop3
  524. {
  525. private $host, $port;
  526. public function __construct($host, $port = null)
  527. {
  528. // no auto connect in this class
  529. $this->host = $host;
  530. $this->port = $port;
  531. }
  532. public function knock($port)
  533. {
  534. $sock = @fsockopen($this->host, $port);
  535. if ($sock) {
  536. fclose($sock);
  537. }
  538. }
  539. public function connect($host = null, $port = null, $ssl = false)
  540. {
  541. if ($host === null) {
  542. $host = $this->host;
  543. }
  544. if ($port === null) {
  545. $port = $this->port;
  546. }
  547. parent::connect($host, $port);
  548. }
  549. }
  550. class Example_Mail_Pop3_Knock extends Zend_Mail_Storage_Pop3
  551. {
  552. public function __construct(array $params)
  553. {
  554. // ... check $params here! ...
  555. $protocol = new Example_Mail_Protocol_Pop3_Knock($params['host']);
  556. // do our "special" thing
  557. foreach ((array)$params['knock_ports'] as $port) {
  558. $protocol->knock($port);
  559. }
  560. // get to correct state
  561. $protocol->connect($params['host'], $params['port']);
  562. $protocol->login($params['user'], $params['password']);
  563. // initialize parent
  564. parent::__construct($protocol);
  565. }
  566. }
  567. $mail = new Example_Mail_Pop3_Knock(array('host' => 'localhost',
  568. 'user' => 'test',
  569. 'password' => 'test',
  570. 'knock_ports' =>
  571. array(1101, 1105, 1111)));
  572. ]]></programlisting>
  573. <para>
  574. As you see, we always assume we're connected, logged in and, if supported, a folder is selected in the
  575. constructor of the main class. Thus if you assign your own protocol class, you always need to make sure
  576. that's done or the next method will fail if the server doesn't allow it in the current state.
  577. </para>
  578. </sect3>
  579. <sect3 id="zend.mail.read-advanced.quota">
  580. <title>Using Quota (since 1.5)</title>
  581. <para>
  582. <classname>Zend_Mail_Storage_Writable_Maildir</classname> has support for Maildir++ quotas. It's disabled by default,
  583. but it's possible to use it manually, if the automatic checks are not desired (this means
  584. <code>appendMessage()</code>, <code>removeMessage()</code> and <code>copyMessage()</code> do no checks
  585. and do not add entries to the maildirsize file). If enabled, an exception is thrown if you try to write
  586. to the maildir and it's already over quota.
  587. </para>
  588. <para>
  589. There are three methods used for quotas: <code>getQuota()</code>, <code>setQuota()</code> and
  590. <code>checkQuota()</code>:
  591. </para>
  592. <programlisting language="php"><![CDATA[
  593. $mail = new Zend_Mail_Storage_Writable_Maildir(array('dirname' =>
  594. '/home/test/mail/'));
  595. $mail->setQuota(true); // true to enable, false to disable
  596. echo 'Quota check is now ', $mail->getQuota() ? 'enabled' : 'disabled', "\n";
  597. // check quota can be used even if quota checks are disabled
  598. echo 'You are ', $mail->checkQuota() ? 'over quota' : 'not over quota', "\n";
  599. ]]></programlisting>
  600. <para>
  601. <code>checkQuota()</code> can also return a more detailed response:
  602. </para>
  603. <programlisting language="php"><![CDATA[
  604. $quota = $mail->checkQuota(true);
  605. echo 'You are ', $quota['over_quota'] ? 'over quota' : 'not over quota', "\n";
  606. echo 'You have ',
  607. $quota['count'],
  608. ' of ',
  609. $quota['quota']['count'],
  610. ' messages and use ';
  611. echo $quota['size'], ' of ', $quota['quota']['size'], ' octets';
  612. ]]></programlisting>
  613. <para>
  614. If you want to specify your own quota instead of using the one specified in the maildirsize file you
  615. can do with <code>setQuota()</code>:
  616. </para>
  617. <programlisting language="php"><![CDATA[
  618. // message count and octet size supported, order does matter
  619. $quota = $mail->setQuota(array('size' => 10000, 'count' => 100));
  620. ]]></programlisting>
  621. <para>
  622. To add your own quota checks use single letters as keys, and they will be preserved (but obviously not checked).
  623. It's also possible to extend <classname>Zend_Mail_Storage_Writable_Maildir</classname> to define your own quota only
  624. if the maildirsize file is missing (which can happen in Maildir++):
  625. </para>
  626. <programlisting language="php"><![CDATA[
  627. class Example_Mail_Storage_Maildir extends Zend_Mail_Storage_Writable_Maildir {
  628. // getQuota is called with $fromStorage = true by quota checks
  629. public function getQuota($fromStorage = false) {
  630. try {
  631. return parent::getQuota($fromStorage);
  632. } catch (Zend_Mail_Storage_Exception $e) {
  633. if (!$fromStorage) {
  634. // unknown error:
  635. throw $e;
  636. }
  637. // maildirsize file must be missing
  638. list($count, $size) = get_quota_from_somewhere_else();
  639. return array('count' => $count, 'size' => $size);
  640. }
  641. }
  642. }
  643. ]]></programlisting>
  644. </sect3>
  645. </sect2>
  646. </sect1>
  647. <!--
  648. vim:se ts=4 sw=4 et:
  649. -->