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

Documentation generated on Mon, 11 Mar 2019 15:56:41 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.