Zend_Mail_Read.xml 31 KB

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