Net_IMAP
[ class tree: Net_IMAP ] [ index: Net_IMAP ] [ all elements ]

Source for file test_IMAP.php

Documentation is available at test_IMAP.php

  1. <?
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>       |
  17. // +----------------------------------------------------------------------+
  18.  
  19.  
  20. /*
  21. This sample shows the use of the IMAP methods
  22. this is only useful for testing and to high level IMAP access example use
  23. */
  24.  
  25.  
  26. include_once('Net/IMAP.php');
  27.  
  28. error_reporting(E_ALL);
  29.  
  30. $user="user";
  31. $passwd="password";
  32. $host="localhost";
  33. $port="143";
  34.  
  35.  
  36.  
  37. //you can create a file called passwords.php and store your $user,$pass,$host and $port values in it
  38. // or you can modify this script
  39. @require_once("../passwords.php");
  40.  
  41.  
  42. $imap= new  Net_IMAP($host,$port);
  43. //The the Protocol debug feature
  44. //$imap->setDebug();
  45. //$imap->setUnparsedResponse(true);
  46.  
  47. //print_r($imap->cmdCapability());
  48. // Login to the IMAP Server Using plain passwords ($authMethod=false)
  49. // $authMethod can be true (dafault) , false or a string
  50.  
  51. /*$authMethod=false;
  52. if ( PEAR::isError( $ret = $imap->login( $user , $passwd , $authMethod ) ) ) {
  53.     echo "Unable to login! reason:" . $ret->getMessage() . "\n";
  54.     exit();
  55. }
  56. */
  57.  
  58.  
  59.  
  60. if ( PEAR::isError( $ret = $imap->login( $user , $passwd  ) ) ) {
  61.     echo "Unable to login! reason:" . $ret->getMessage() . "\n";
  62.     exit();
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. /*********************
  71. ***
  72.     Let's show the Mailbox  related methods
  73. ***
  74. *********************/
  75.  
  76.  
  77.  
  78. $imap->selectMailbox('inbox');
  79.  
  80. //$mailboxes=$imap->getMailboxes('');
  81. $mailboxes=$imap->getMailboxes('inbox.INBOX2',2);
  82. $mailboxes=$imap->getMailboxes('inbox');
  83. //$mailboxes=$imap->listsubscribedMailboxes('inbox');
  84.  
  85. echo "Here is the list of all mailboxes:\n\n";
  86. prettyMailboxList($imap,$mailboxes);
  87.  
  88.  
  89.  
  90.  
  91.  
  92. $mailboxes=$imap->listsubscribedMailboxes('inbox');
  93. echo "Here is the list of all mailboxes you are subscribed:\n\n";
  94. prettyMailboxList($imap,$mailboxes);
  95.  
  96.  
  97.  
  98.  
  99. //$mailboxes=0;
  100.  
  101.  
  102.  
  103. //echo "PITERROR|" . print_r( $imap->_socket->eof()) . "|\n";
  104.  
  105. //echo $imap->getDebugDialog();
  106. //exit();
  107.  
  108.  
  109.  
  110. $folder_delim=$imap->getHierarchyDelimiter();
  111. echo "Folder Delim:|$folder_delim|\n";
  112. $mailbox='INBOX'.$folder_delim .'INBOX2';
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. echo "Getting the summary of message 1\n";
  121.  
  122. $aa=$imap->getSummary(1);
  123. //print_r($aa);
  124.  
  125.  
  126. $aaa=$imap->examineMailbox("inbox");
  127. //print_r($aaa);
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. echo "creating mailbox $mailbox ....";
  139.  
  140. if( ! $ret = $imap->createMailbox($mailbox) ){
  141.     echo "\nCan't create the mailbox '$mailbox' because " . $ret->getMessage() . "\n";
  142. } else{
  143.     echo "OK!\n";
  144. }
  145.  
  146.  
  147.  
  148.  
  149. echo "\n\n\n+-----------------------------------------------------------------------------+\n";
  150.  
  151.  
  152.  
  153.  
  154.  
  155. //$mailbox='INBOX.INBOX2';
  156.  
  157.  
  158.  
  159. //print_r($imap->cmdList("","*"));
  160.  
  161.  
  162.  
  163. //$mailbox='INBOX'.$folder_delim .'INBOX2';
  164.  
  165. if( $imap->mailboxExist($mailbox) ){
  166.     echo "The mailbox $mailbox exists\n";
  167. }else{
  168.     echo "The mailbox $mailbox don't exists!\n";
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. $email="From: <damian@cnba.uba.ar>\r\n";
  179. $email.="To: <damian@localhost>\r\n";
  180. $email.="Subject: Test\r\n";
  181. $email.="\r\n";
  182. $email.="\r\n";
  183. $email.="test\r\n";
  184.  
  185.  
  186.  
  187.  
  188. echo "APPEND\n";
  189. //$imap->cmdAppend("inbox",$email);
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. $mailbox='inbox';
  200.  
  201. echo "Now lets check the flags of messages in $mailbox\n";
  202.  
  203.  
  204.  
  205.  
  206. if ( !PEAR::isError( $num_messages = $imap->getNumberOfMessages( $mailbox  ) ) ){
  207.  
  208.  
  209.  
  210.     for($i=1; $i<=$num_messages;$i++) {
  211.     
  212.         print_r($imap->getFlags($i));
  213.         //echo "AAAA\n";
  214.     /*    
  215.         if ($imap->isSeen($i)) {
  216.                 echo "message $i has been read before...<br>\n";
  217.                 //$msg = $imap->getMsg($i);
  218.                 #echo $msg;
  219.             }
  220.         if ($imap->isFlagged($i)) {
  221.                 echo "message $i has been Flagged...<br>\n";
  222.                 //$msg = $imap->getMsg($i);
  223.                 #echo $msg;
  224.             }
  225.         if ($imap->isDeleted($i)) {
  226.                 echo "message $i is marked as Deleted...<br>\n";
  227.                 //$msg = $imap->getMsg($i);
  228.                 #echo $msg;
  229.             }
  230.     */
  231.     }
  232.  
  233. }else{
  234.     echo "Or $mailbox has no messages or there was an error!\n";
  235. }
  236.  
  237.  
  238.  
  239.  
  240. $imap->selectMailbox('inbox');
  241.  
  242. $nummsg = $imap->getNumberOfMessages();
  243.  
  244.  
  245.  
  246. for($i=1; $i<=$nummsg;$i++) {
  247.     if ($imap->isSeen($i)) {
  248.         echo "message $i has been read before...<br>\n";
  249.         //$msg = $imap->getMsg($i);
  250.         #echo $msg;
  251.     }
  252.    if ($imap->isFlagged($i)) {
  253.         echo "message $i has been Flagged...<br>\n";
  254.         //$msg = $imap->getMsg($i);
  255.         #echo $msg;
  256.     }
  257.    if ($imap->isDeleted($i)) {
  258.         echo "message $i is marked as Deleted...<br>\n";
  259.         //$msg = $imap->getMsg($i);
  260.         #echo $msg;
  261.     }
  262.  
  263.     
  264. }
  265.  
  266.  
  267.  
  268.  
  269. /*
  270.  
  271. echo "renaming mailbox INBOX2 to INBOX3 : <br>\n";
  272. $imap->renameMailbox('INBOX2', 'INBOX3');
  273.  
  274.  
  275.  
  276.  
  277.  
  278. echo "deleting mailbox INBOX3 : <br>\n";
  279. $imap->deleteMailbox('INBOX3');
  280. //echo 'deleting msg 1  : <br>\n';
  281. //$imap->delete(1);
  282. echo "creating mailbox TESTING : <br>\n";
  283. $imap->createMailbox('TESTING');
  284. echo "copying msg 1 INBOX to TESTING :<br>\n";
  285. $imap->copyMessages(1, 'TESTING');
  286.  
  287.  
  288. */
  289.  
  290.  
  291.  
  292.  
  293.  
  294. // Get the raw headers of message 1
  295. echo "<h2>getRawHeaders()</h2>\n";
  296. echo "<pre>" . htmlspecialchars($imap->getRawHeaders(1)) . "</pre>\n";
  297.  
  298.  
  299. //* Get structured headers of message 1
  300. echo "<h2>getParsedHeaders()</h2> <pre>\n";
  301. print_r($imap->getParsedHeaders(1));
  302. echo "</pre>\n";
  303.  
  304.  
  305.  
  306. //* Get body of message 1
  307. echo "<h2>getBody()</h2>\n";
  308. echo "<pre>" . htmlspecialchars($imap->getBody(1)) . "</pre>\n";
  309.  
  310.  
  311.  
  312. //* Get number of messages in maildrop
  313. echo "<h2>getNumMsg</h2>\n";
  314. echo "<pre>" . $imap->numMsg('') . "</pre>\n";
  315.  
  316.  
  317.  
  318.  
  319. //* Get entire message
  320. echo "<h2>getMsg()</h2>\n";
  321.  
  322.  
  323. if(!PEAR::isError($msg=$imap->getMsg(1))){
  324. print_r($msg);
  325.     echo '<pre>' . htmlspecialchars($msg) . '</pre>\n';
  326. }
  327.  
  328.  
  329. //* Get listing details of the maildrop
  330. echo "<h2>getListing()</h2>\n";
  331. echo "<pre>\n";
  332. print_r($imap->getListing());
  333. echo "</pre>\n";
  334.  
  335.  
  336. //* Get size of maildrop
  337. echo "<h2>getSize()</h2>\n";
  338. echo "<pre>" . $imap->getSize() . "</pre>\n";
  339.  
  340.  
  341.  
  342.  
  343. //* Delete a msg
  344.  
  345. //echo '<h2>delete()</h2>\n';
  346. // Use with CARE!!!
  347. //echo '<pre>' . $imap->deleteMsg(1) . '</pre>\n';
  348.  
  349.  
  350.  
  351. $mailbox="inbox";
  352.  
  353. $imap->selectMailbox($mailbox);
  354.  
  355. $nummsg=$imap->numMsg();
  356. echo "You have $nummsg in $mailbox folder\n";
  357. //echo "See header in message number 1: " . $imap->top(1) . '<br>';
  358. echo "See header in message number 1: " . htmlspecialchars($imap->getRawHeaders(1)) . '<br>\n';
  359.  
  360. if(!PEAR::isError($msg=$imap->getMsg(1))){
  361. print_r($msg);
  362.     echo "Read message number 1: " . htmlspecialchars($msg) . '<br>\n';
  363. }
  364.  
  365.  
  366.  
  367. for($i=1; $i<=$nummsg;$i++) {
  368.     if ($imap->isSeen($i)) {
  369.         echo "message $i has been read before...<br>\n";
  370.         //$msg = $imap->getMsg($i);
  371.         #echo $msg;
  372.     }
  373.    if ($imap->isFlagged($i)) {
  374.         echo "message $i has been Flagged...<br>\n";
  375.         //$msg = $imap->getMsg($i);
  376.         #echo $msg;
  377.     }
  378.    if ($imap->isDeleted($i)) {
  379.         echo "message $i is marked as Deleted...<br>\n";
  380.         //$msg = $imap->getMsg($i);
  381.         #echo $msg;
  382.     }
  383.  
  384.  
  385. }
  386.  
  387. //print_r($imap->getMailboxes(''));
  388.  
  389. echo "creating mailbox INBOX2 : <br>\n";
  390. $imap->createMailbox('INBOX2');
  391. echo "renaming mailbox INBOX2 to INBOX3 : <br>\n";
  392. $imap->renameMailbox('INBOX2', 'INBOX3');
  393.  
  394. echo "deleting mailbox INBOX3 : <br>\n";
  395. $imap->deleteMailbox('INBOX3');
  396. //echo 'deleting msg 1  : <br>\n';
  397. //$imap->delete(1);
  398. echo "creating mailbox TESTING : <br>\n";
  399. $imap->createMailbox('TESTING');
  400. echo "copying msg 1 INBOX to TESTING :<br>\n";
  401. $imap->copyMessages('TESTING', 1);
  402.  
  403.  
  404. //* Disconnect
  405.  
  406. $imap->disconnect();
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415. function prettyMailboxList($imap,$mailboxes){
  416.  
  417.  
  418.  
  419.     if( count($mailboxes) > 0 ){
  420.  
  421.         echo "You have " . count($mailboxes) . " Mailboxes\n\n";
  422.  
  423.  
  424.         echo "+-----------------------------------------------------------------------------+\n";
  425.         echo "|Mailbox                                           | Mbox Size  | Cant Mesages|\n";
  426.         echo "+-----------------------------------------------------------------------------+\n";
  427.  
  428.         foreach($mailboxes as $mailbox){
  429.  
  430.             if ( PEAR::isError( $mbox_size =$imap->getMailboxSize( $mailbox  ) ) ){
  431.                 //echo "Unable to retr msg size" . $mbox_size->getMessage() . "|\n";
  432.                 $mbox_size="[ERROR]";
  433.             }
  434.             //print_r($mbox_size);
  435.             if ( PEAR::isError( $num_messages = $imap->getNumberOfMessages( $mailbox  ) ) ){
  436.                 //echo "Unable to rert msg" . $num_messages->getMessage() . "|\n";
  437.                 $num_messages="[ERROR]";
  438.             }
  439.  
  440.  
  441.             echo "|";
  442.             echo $mailbox;
  443.             // Align the output
  444.             for($i=strlen($mailbox) ; $i< 50 ; $i++)  echo ' ';
  445.             echo "|";
  446.             // Align the output
  447.             for($i=strlen($mbox_size) ; $i< 12 ; $i++)  echo ' ';
  448.             echo $mbox_size;
  449.  
  450.  
  451.             echo "|";
  452.             // Align the output
  453.             //print_r($num_messages);
  454.             for($i=strlen($num_messages) ; $i< 13 ; $i++)  echo ' ';
  455.             echo $num_messages;
  456.  
  457.  
  458.  
  459.             echo "|";
  460.             echo "\n";
  461.  
  462.         }
  463.         echo "+-----------------------------------------------------------------------------+\n";
  464.     }else{
  465.         echo "Warning!:\n   You have any mailboxes!!\n";
  466.     }
  467. }
  468. ?>

Documentation generated on Mon, 11 Mar 2019 13:54:09 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.