Zend_Mail_Read.xml 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. <code>isMultipart</code> 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 <code>RecursiveIterator</code>,
  299. which makes it easy to scan through all parts. And for easy output, it also implements
  300. the magic method <methodname>__toString()</methodname>, which returns the content.
  301. </para>
  302. <programlisting language="php"><![CDATA[
  303. // output first text/plain part
  304. $foundPart = null;
  305. foreach (new RecursiveIteratorIterator($mail->getMessage(1)) as $part) {
  306. try {
  307. if (strtok($part->contentType, ';') == 'text/plain') {
  308. $foundPart = $part;
  309. break;
  310. }
  311. } catch (Zend_Mail_Exception $e) {
  312. // ignore
  313. }
  314. }
  315. if (!$foundPart) {
  316. echo 'no plain text part found';
  317. } else {
  318. echo "plain text part: \n" . $foundPart;
  319. }
  320. ]]></programlisting>
  321. </sect2>
  322. <sect2 id="zend.mail.read-flags">
  323. <title>Checking for flags</title>
  324. <para>
  325. Maildir and IMAP support storing flags. The class
  326. <classname>Zend_Mail_Storage</classname> has constants for all known maildir and IMAP
  327. system flags, named <classname>Zend_Mail_Storage::FLAG_&lt;flagname&gt;</classname>. To
  328. check for flags <classname>Zend_Mail_Message</classname> has a method called
  329. <methodname>hasFlag()</methodname>. With <methodname>getFlags()</methodname> you'll get
  330. all set flags.
  331. </para>
  332. <programlisting language="php"><![CDATA[
  333. // find unread messages
  334. echo "Unread mails:\n";
  335. foreach ($mail as $message) {
  336. if ($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) {
  337. continue;
  338. }
  339. // mark recent/new mails
  340. if ($message->hasFlag(Zend_Mail_Storage::FLAG_RECENT)) {
  341. echo '! ';
  342. } else {
  343. echo ' ';
  344. }
  345. echo $message->subject . "\n";
  346. }
  347. // check for known flags
  348. $flags = $message->getFlags();
  349. echo "Message is flagged as: ";
  350. foreach ($flags as $flag) {
  351. switch ($flag) {
  352. case Zend_Mail_Storage::FLAG_ANSWERED:
  353. echo 'Answered ';
  354. break;
  355. case Zend_Mail_Storage::FLAG_FLAGGED:
  356. echo 'Flagged ';
  357. break;
  358. // ...
  359. // check for other flags
  360. // ...
  361. default:
  362. echo $flag . '(unknown flag) ';
  363. }
  364. }
  365. ]]></programlisting>
  366. <para>
  367. As IMAP allows user or client defined flags, you could get flags that don't have a
  368. constant in <classname>Zend_Mail_Storage</classname>. Instead, they are returned as
  369. strings and can be checked the same way with <methodname>hasFlag()</methodname>.
  370. </para>
  371. <programlisting language="php"><![CDATA[
  372. // check message for client defined flags $IsSpam, $SpamTested
  373. if (!$message->hasFlag('$SpamTested')) {
  374. echo 'message has not been tested for spam';
  375. } else if ($message->hasFlag('$IsSpam')) {
  376. echo 'this message is spam';
  377. } else {
  378. echo 'this message is ham';
  379. }
  380. ]]></programlisting>
  381. </sect2>
  382. <sect2 id="zend.mail.read-folders">
  383. <title>Using folders</title>
  384. <para>
  385. All storages, except Pop3, support folders, also called mailboxes. The interface
  386. implemented by all storages supporting folders is called
  387. <classname>Zend_Mail_Storage_Folder_Interface</classname>. Also all of these classes
  388. have an additional optional parameter called <code>folder</code>, which is the folder
  389. selected after login, in the constructor.
  390. </para>
  391. <para>
  392. For the local storages you need to use separate classes called
  393. <classname>Zend_Mail_Storage_Folder_Mbox</classname> or
  394. <classname>Zend_Mail_Storage_Folder_Maildir</classname>. Both need one parameter called
  395. <code>dirname</code> with the name of the base dir. The format for maildir is as defined
  396. in maildir++ (with a dot as default delimiter), Mbox is a directory hierarchy with Mbox
  397. files. If you don't have a Mbox file called INBOX in your Mbox base dir you need to set
  398. another folder in the constructor.
  399. </para>
  400. <para>
  401. <classname>Zend_Mail_Storage_Imap</classname> already supports folders by default.
  402. Examples for opening these storages:
  403. </para>
  404. <programlisting language="php"><![CDATA[
  405. // mbox with folders
  406. $mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' =>
  407. '/home/test/mail/'));
  408. // mbox with a default folder not called INBOX, also works
  409. // with Zend_Mail_Storage_Folder_Maildir and Zend_Mail_Storage_Imap
  410. $mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' =>
  411. '/home/test/mail/',
  412. 'folder' =>
  413. 'Archive'));
  414. // maildir with folders
  415. $mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' =>
  416. '/home/test/mail/'));
  417. // maildir with colon as delimiter, as suggested in Maildir++
  418. $mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' =>
  419. '/home/test/mail/',
  420. 'delim' => ':'));
  421. // imap is the same with and without folders
  422. $mail = new Zend_Mail_Storage_Imap(array('host' => 'example.com',
  423. 'user' => 'test',
  424. 'password' => 'test'));
  425. ]]></programlisting>
  426. <para>
  427. With the method getFolders($root = null) you can get the folder hierarchy starting with
  428. the root folder or the given folder. It's returned as an instance of
  429. <classname>Zend_Mail_Storage_Folder</classname>, which implements
  430. <code>RecursiveIterator</code> and all children are also instances of
  431. <classname>Zend_Mail_Storage_Folder</classname>. Each of these instances has a local and
  432. a global name returned by the methods <methodname>getLocalName()</methodname> and
  433. <methodname>getGlobalName()</methodname>. The global name is the absolute name from the
  434. root folder (including delimiters), the local name is the name in the parent folder.
  435. </para>
  436. <table id="zend.mail.read-folders.table-1">
  437. <title>Mail Folder Names</title>
  438. <tgroup cols="2">
  439. <thead>
  440. <row>
  441. <entry>Global Name</entry>
  442. <entry>Local Name</entry>
  443. </row>
  444. </thead>
  445. <tbody>
  446. <row>
  447. <entry>/INBOX</entry>
  448. <entry>INBOX</entry>
  449. </row>
  450. <row>
  451. <entry>/Archive/2005</entry>
  452. <entry>2005</entry>
  453. </row>
  454. <row>
  455. <entry>List.ZF.General</entry>
  456. <entry>General</entry>
  457. </row>
  458. </tbody>
  459. </tgroup>
  460. </table>
  461. <para>
  462. If you use the iterator, the key of the current element is the local name. The global
  463. name is also returned by the magic method <methodname>__toString()</methodname>. Some
  464. folders may not be selectable, which means they can't store messages and selecting them
  465. results in an error. This can be checked with the method
  466. <methodname>isSelectable()</methodname>. So it's very easy to output the whole tree in a
  467. view:
  468. </para>
  469. <programlisting language="php"><![CDATA[
  470. $folders = new RecursiveIteratorIterator($this->mail->getFolders(),
  471. RecursiveIteratorIterator::SELF_FIRST);
  472. echo '<select name="folder">';
  473. foreach ($folders as $localName => $folder) {
  474. $localName = str_pad('', $folders->getDepth(), '-', STR_PAD_LEFT) .
  475. $localName;
  476. echo '<option';
  477. if (!$folder->isSelectable()) {
  478. echo ' disabled="disabled"';
  479. }
  480. echo ' value="' . htmlspecialchars($folder) . '">'
  481. . htmlspecialchars($localName) . '</option>';
  482. }
  483. echo '</select>';
  484. ]]></programlisting>
  485. <para>
  486. The current selected folder is returned by the method
  487. <methodname>getSelectedFolder()</methodname>. Changing the folder is done with the
  488. method <methodname>selectFolder()</methodname>, which needs the global name as
  489. parameter. If you want to avoid to write delimiters you can also use the properties of a
  490. <classname>Zend_Mail_Storage_Folder</classname> instance:
  491. </para>
  492. <programlisting language="php"><![CDATA[
  493. // depending on your mail storage and its settings $rootFolder->Archive->2005
  494. // is the same as:
  495. // /Archive/2005
  496. // Archive:2005
  497. // INBOX.Archive.2005
  498. // ...
  499. $folder = $mail->getFolders()->Archive->2005;
  500. echo 'Last folder was '
  501. . $mail->getSelectedFolder()
  502. . "new folder is $folder\n";
  503. $mail->selectFolder($folder);
  504. ]]></programlisting>
  505. </sect2>
  506. <sect2 id="zend.mail.read-advanced">
  507. <title>Advanced Use</title>
  508. <sect3 id="zend.mail.read-advanced.noop">
  509. <title>Using NOOP</title>
  510. <para>
  511. If you're using a remote storage and have some long tasks you might need to keep
  512. the connection alive via noop:
  513. </para>
  514. <programlisting language="php"><![CDATA[
  515. foreach ($mail as $message) {
  516. // do some calculations ...
  517. $mail->noop(); // keep alive
  518. // do something else ...
  519. $mail->noop(); // keep alive
  520. }
  521. ]]></programlisting>
  522. </sect3>
  523. <sect3 id="zend.mail.read-advanced.caching">
  524. <title>Caching instances</title>
  525. <para>
  526. <classname>Zend_Mail_Storage_Mbox</classname>,
  527. <classname>Zend_Mail_Storage_Folder_Mbox</classname>,
  528. <classname>Zend_Mail_Storage_Maildir</classname> and
  529. <classname>Zend_Mail_Storage_Folder_Maildir</classname> implement the magic methods
  530. <methodname>__sleep()</methodname> and <methodname>__wakeup()</methodname>, which
  531. means they are serializable. This avoids parsing the files or directory tree more
  532. than once. The disadvantage is that your Mbox or Maildir storage should not change.
  533. Some easy checks may be done, like reparsing the current Mbox file if the
  534. modification time changes, or reparsing the folder structure if a folder has
  535. vanished (which still results in an error, but you can search for another folder
  536. afterwards). It's better if you have something like a signal file for changes and
  537. check it before using the cached instance.
  538. </para>
  539. <programlisting language="php"><![CDATA[
  540. // there's no specific cache handler/class used here,
  541. // change the code to match your cache handler
  542. $signal_file = '/home/test/.mail.last_change';
  543. $mbox_basedir = '/home/test/mail/';
  544. $cache_id = 'example mail cache ' . $mbox_basedir . $signal_file;
  545. $cache = new Your_Cache_Class();
  546. if (!$cache->isCached($cache_id) ||
  547. filemtime($signal_file) > $cache->getMTime($cache_id)) {
  548. $mail = new Zend_Mail_Storage_Folder_Pop3(array('dirname' =>
  549. $mbox_basedir));
  550. } else {
  551. $mail = $cache->get($cache_id);
  552. }
  553. // do stuff ...
  554. $cache->set($cache_id, $mail);
  555. ]]></programlisting>
  556. </sect3>
  557. <sect3 id="zend.mail.read-advanced.extending">
  558. <title>Extending Protocol Classes</title>
  559. <para>
  560. Remote storages use two classes:
  561. <classname>Zend_Mail_Storage_&lt;Name&gt;</classname> and
  562. <classname>Zend_Mail_Protocol_&lt;Name&gt;</classname>. The protocol class
  563. translates the protocol commands and responses from and to <acronym>PHP</acronym>,
  564. like methods for the commands or variables with different structures for data.
  565. The other/main class implements the common interface.
  566. </para>
  567. <para>
  568. If you need additional protocol features, you can extend the protocol class and use
  569. it in the constructor of the main class. As an example, assume we need to knock
  570. different ports before we can connect to POP3.
  571. </para>
  572. <programlisting language="php"><![CDATA[
  573. class Example_Mail_Exception extends Zend_Mail_Exception
  574. {
  575. }
  576. class Example_Mail_Protocol_Exception extends Zend_Mail_Protocol_Exception
  577. {
  578. }
  579. class Example_Mail_Protocol_Pop3_Knock extends Zend_Mail_Protocol_Pop3
  580. {
  581. private $host, $port;
  582. public function __construct($host, $port = null)
  583. {
  584. // no auto connect in this class
  585. $this->host = $host;
  586. $this->port = $port;
  587. }
  588. public function knock($port)
  589. {
  590. $sock = @fsockopen($this->host, $port);
  591. if ($sock) {
  592. fclose($sock);
  593. }
  594. }
  595. public function connect($host = null, $port = null, $ssl = false)
  596. {
  597. if ($host === null) {
  598. $host = $this->host;
  599. }
  600. if ($port === null) {
  601. $port = $this->port;
  602. }
  603. parent::connect($host, $port);
  604. }
  605. }
  606. class Example_Mail_Pop3_Knock extends Zend_Mail_Storage_Pop3
  607. {
  608. public function __construct(array $params)
  609. {
  610. // ... check $params here! ...
  611. $protocol = new Example_Mail_Protocol_Pop3_Knock($params['host']);
  612. // do our "special" thing
  613. foreach ((array)$params['knock_ports'] as $port) {
  614. $protocol->knock($port);
  615. }
  616. // get to correct state
  617. $protocol->connect($params['host'], $params['port']);
  618. $protocol->login($params['user'], $params['password']);
  619. // initialize parent
  620. parent::__construct($protocol);
  621. }
  622. }
  623. $mail = new Example_Mail_Pop3_Knock(array('host' => 'localhost',
  624. 'user' => 'test',
  625. 'password' => 'test',
  626. 'knock_ports' =>
  627. array(1101, 1105, 1111)));
  628. ]]></programlisting>
  629. <para>
  630. As you see, we always assume we're connected, logged in and, if supported, a folder
  631. is selected in the constructor of the main class. Thus if you assign your own
  632. protocol class, you always need to make sure that's done or the next method will
  633. fail if the server doesn't allow it in the current state.
  634. </para>
  635. </sect3>
  636. <sect3 id="zend.mail.read-advanced.quota">
  637. <title>Using Quota (since 1.5)</title>
  638. <para>
  639. <classname>Zend_Mail_Storage_Writable_Maildir</classname> has support for Maildir++
  640. quotas. It's disabled by default, but it's possible to use it manually, if the
  641. automatic checks are not desired (this means
  642. <methodname>appendMessage()</methodname>, <methodname>removeMessage()</methodname>
  643. and <methodname>copyMessage()</methodname> do no checks and do not add entries to
  644. the maildirsize file). If enabled, an exception is thrown if you try to write to the
  645. maildir and it's already over quota.
  646. </para>
  647. <para>
  648. There are three methods used for quotas: <methodname>getQuota()</methodname>,
  649. <methodname>setQuota()</methodname> and <methodname>checkQuota()</methodname>:
  650. </para>
  651. <programlisting language="php"><![CDATA[
  652. $mail = new Zend_Mail_Storage_Writable_Maildir(array('dirname' =>
  653. '/home/test/mail/'));
  654. $mail->setQuota(true); // true to enable, false to disable
  655. echo 'Quota check is now ', $mail->getQuota() ? 'enabled' : 'disabled', "\n";
  656. // check quota can be used even if quota checks are disabled
  657. echo 'You are ', $mail->checkQuota() ? 'over quota' : 'not over quota', "\n";
  658. ]]></programlisting>
  659. <para>
  660. <methodname>checkQuota()</methodname> can also return a more detailed response:
  661. </para>
  662. <programlisting language="php"><![CDATA[
  663. $quota = $mail->checkQuota(true);
  664. echo 'You are ', $quota['over_quota'] ? 'over quota' : 'not over quota', "\n";
  665. echo 'You have ',
  666. $quota['count'],
  667. ' of ',
  668. $quota['quota']['count'],
  669. ' messages and use ';
  670. echo $quota['size'], ' of ', $quota['quota']['size'], ' octets';
  671. ]]></programlisting>
  672. <para>
  673. If you want to specify your own quota instead of using the one specified in the
  674. maildirsize file you can do with <methodname>setQuota()</methodname>:
  675. </para>
  676. <programlisting language="php"><![CDATA[
  677. // message count and octet size supported, order does matter
  678. $quota = $mail->setQuota(array('size' => 10000, 'count' => 100));
  679. ]]></programlisting>
  680. <para>
  681. To add your own quota checks use single letters as keys, and they will be preserved
  682. (but obviously not checked). It's also possible to extend
  683. <classname>Zend_Mail_Storage_Writable_Maildir</classname> to define your own quota
  684. only if the maildirsize file is missing (which can happen in Maildir++):
  685. </para>
  686. <programlisting language="php"><![CDATA[
  687. class Example_Mail_Storage_Maildir extends Zend_Mail_Storage_Writable_Maildir {
  688. // getQuota is called with $fromStorage = true by quota checks
  689. public function getQuota($fromStorage = false) {
  690. try {
  691. return parent::getQuota($fromStorage);
  692. } catch (Zend_Mail_Storage_Exception $e) {
  693. if (!$fromStorage) {
  694. // unknown error:
  695. throw $e;
  696. }
  697. // maildirsize file must be missing
  698. list($count, $size) = get_quota_from_somewhere_else();
  699. return array('count' => $count, 'size' => $size);
  700. }
  701. }
  702. }
  703. ]]></programlisting>
  704. </sect3>
  705. </sect2>
  706. </sect1>
  707. <!--
  708. vim:se ts=4 sw=4 et:
  709. -->