Back to index

php5  5.3.10
imap_include.inc
Go to the documentation of this file.
00001 <?php
00002 // Change these to make tests run successfully
00003 $server   = '{localhost/norsh}';
00004 $default_mailbox = $server . "INBOX";
00005 $domain = "something.com";
00006 $admin_user = "webmaster"; // a user with admin access 
00007 $username = "$admin_user@$domain";
00008 $password = 'p4ssw0rd';
00009 $users = array("webmaster", "info", "admin", "foo"); // tests require 4 valid userids
00010 $mailbox_prefix = "phpttest"; // name used for test mailbox
00011 
00012 // record test start time (used by displayOverviewFields())
00013 $start_time = time();
00014 
00015 // list of fields to expect
00016 $mandatory_overview_fields = array(
00017                     'size',
00018                     'uid',
00019                     'msgno',
00020                     'recent',
00021                     'flagged',
00022                     'answered',
00023                     'deleted',
00024                     'seen',
00025                     'draft',
00026                     'udate',
00027                    );
00028 
00038 function displayOverviewFields($resp, $fields=null) {
00039   global $mandatory_overview_fields;
00040   global $start_time;
00041 
00042   foreach ($fields ? $fields : $mandatory_overview_fields as $mf)
00043   {
00044     $z = $resp->$mf;
00045     if ($mf == 'udate') {
00046       if (($z >= $start_time) && ($z <= time())) {
00047         echo "$mf is OK\n";
00048       } else {
00049         echo "$mf is BAD ($z)\n";
00050       }
00051     } else {
00052       echo "$mf is $z\n";
00053     }
00054   }
00055 }
00056 
00057 
00066 function setup_test_mailbox($mailbox_suffix, $message_count, &$new_mailbox = null, $msg_type = "simple"){
00067        global $server, $default_mailbox, $username, $password;
00068        
00069        // open a stream to default mailbox
00070        $imap_stream = imap_open($default_mailbox, $username, $password);
00071 
00072        if ($imap_stream === false) {
00073               echo "Cannot connect to IMAP server $server: " . imap_last_error() . "\n";
00074               return false;
00075        }      
00076        
00077        echo "Create a temporary mailbox and add " . $message_count .  " msgs\n";
00078        $new_mailbox = create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type);
00079        if ($new_mailbox === false) {
00080           echo "Cant create a temporary mailbox: " . imap_last_error(). "\n";
00081           return false;
00082        }
00083        
00084        echo ".. mailbox '$new_mailbox' created\n";
00085        
00086        // reopen stream to new mailbox
00087        if (imap_reopen($imap_stream, $new_mailbox) === false) {
00088               echo "cant re-open '$new_mailbox' mailbox: " . imap_last_error() . "\n";
00089               return false;
00090        }
00091        
00092        return $imap_stream;
00093 }
00094 
00101 function create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type= "simple"){
00102        global $default_mailbox, $mailbox_prefix;
00103        $mailbox = $default_mailbox . "." . $mailbox_prefix . $mailbox_suffix;
00104        
00105        $mailboxes = imap_getmailboxes($imap_stream, $mailbox, '*');
00106        
00107        // check mailbox does not already exist
00108        if ($mailboxes) {
00109               foreach($mailboxes as $value) {
00110                      if ($value->name == $mailbox) {
00111                             exit ("TEST FAILED : Mailbox '$mailbox' already exists\n");
00112                      }
00113               }
00114        }      
00115 
00116        if (imap_createmailbox($imap_stream, $mailbox) === false) {
00117               return false;
00118        }
00119        
00120        // Add number of test msgs requested
00121        if ($message_count > 0) {
00122               populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type);
00123        }      
00124        
00125        return $mailbox;
00126 }
00127 
00134 function populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type = "simple"){
00135 
00136        global $users, $domain;
00137 
00138        for($i = 1; $i <= $message_count; $i++) {
00139               if ($msg_type == "simple") {
00140                      $msg =  "From: foo@anywhere.com\r\n"
00141                             . "To: $users[0]@$domain\r\n"
00142                             . "Subject: test$i\r\n"
00143                             . "\r\n"
00144                             . "$i: this is a test message, please ignore\r\n";
00145               } else {
00146                      $envelope["from"]= "foo@anywhere.com";
00147                      $envelope["to"]  = "$users[0]@$domain";
00148                      $envelope["subject"] = "Test msg $i";
00149               
00150                      $part1["type"] = TYPEMULTIPART;
00151                      $part1["subtype"] = "mixed";
00152                      
00153                      $part2["type"] = TYPETEXT;
00154                      $part2["subtype"] = "plain";
00155                      $part2["description"] = "imap_mail_compose() function";
00156                      $part2["contents.data"] = "message 1:xxxxxxxxxxxxxxxxxxxxxxxxxx";
00157                      
00158                      $part3["type"] = TYPETEXT;
00159                      $part3["subtype"] = "plain";
00160                      $part3["description"] = "Example";
00161                      $part3["contents.data"] = "message 2:yyyyyyyyyyyyyyyyyyyyyyyyyy";
00162                      
00163                      $part4["type"] = TYPETEXT;
00164                      $part4["subtype"] = "plain";
00165                      $part4["description"] = "Return Values";
00166                      $part4["contents.data"] = "message 3:zzzzzzzzzzzzzzzzzzzzzzzzzz";
00167                      
00168                      $body[1] = $part1;
00169                      $body[2] = $part2;
00170                      $body[3] = $part3;
00171                      $body[4] = $part4;
00172                      
00173                      $msg = imap_mail_compose($envelope, $body);
00174               }
00175        
00176               imap_append($imap_stream, $mailbox, $msg);
00177        }
00178 }
00179 
00186 function get_mailbox_name($mailbox){
00187 
00188        if (preg_match('/\{.*?\}(.*)/', $mailbox, $match) != 1) {
00189               echo "Unrecpognized mailbox name\n";
00190               return false;
00191        }
00192 
00193        return $match[1];
00194 }
00195 
00196 ?>