Back to index

php5  5.3.10
mail_include.inc
Go to the documentation of this file.
00001 <?php
00002 // Change these to make tests run successfully
00003 $server   = '{localhost}';
00004 $default_mailbox = $server . "INBOX";
00005 $domain = "example.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 
00020 function setup_test_mailbox($mailbox_suffix, $message_count, &$new_mailbox = null, $msg_type = "simple"){
00021        global $server, $default_mailbox, $username, $password;
00022        
00023        // open a stream to default mailbox
00024        $imap_stream = imap_open($default_mailbox, $username, $password);
00025 
00026        if ($imap_stream === false) {
00027               echo "Cannot connect to IMAP server $server: " . imap_last_error() . "\n";
00028               return false;
00029        }      
00030        
00031        echo "Create a temporary mailbox and add " . $message_count .  " msgs\n";
00032        $new_mailbox = create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type);
00033        if ($new_mailbox === false) {
00034           echo "Cant create a temporary mailbox: " . imap_last_error(). "\n";
00035           return false;
00036        }
00037        
00038        echo ".. mailbox '$new_mailbox' created\n";
00039        
00040        // reopen stream to new mailbox
00041        if (imap_reopen($imap_stream, $new_mailbox) === false) {
00042               echo "cant re-open '$new_mailbox' mailbox: " . imap_last_error() . "\n";
00043               return false;
00044        }
00045        
00046        return $imap_stream;
00047 }
00048 
00055 function create_mailbox($imap_stream, $mailbox_suffix, $message_count, $msg_type= "simple"){
00056        global $default_mailbox, $mailbox_prefix;
00057        $mailbox = $default_mailbox . "." . $mailbox_prefix . $mailbox_suffix;
00058        
00059        $mailboxes = imap_getmailboxes($imap_stream, $mailbox, '*');
00060        
00061        // check mailbox does not already exist
00062        if ($mailboxes) {
00063               foreach($mailboxes as $value) {
00064                      if ($value->name == $mailbox) {
00065                             exit ("TEST FAILED : Mailbox '$mailbox' already exists\n");
00066                      }
00067               }
00068        }      
00069 
00070        if (imap_createmailbox($imap_stream, $mailbox) === false) {
00071               return false;
00072        }
00073        
00074        // Add number of test msgs requested
00075        if ($message_count > 0) {
00076               populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type);
00077        }      
00078        
00079        return $mailbox;
00080 }
00081 
00088 function populate_mailbox($imap_stream, $mailbox, $message_count, $msg_type = "simple"){
00089 
00090        global $users, $domain;
00091 
00092        for($i = 1; $i <= $message_count; $i++) {
00093               if ($msg_type == "simple") {
00094                      $msg =  "From: foo@anywhere.com\r\n"
00095                             . "To: $users[0]@$domain\r\n"
00096                             . "Subject: test$i\r\n"
00097                             . "\r\n"
00098                             . "$i: this is a test message, please ignore\r\n";
00099               } else {
00100                      $envelope["from"]= "foo@anywhere.com";
00101                      $envelope["to"]  = "$users[0]@$domain";
00102                      $envelope["subject"] = "Test msg $i";
00103               
00104                      $part1["type"] = TYPEMULTIPART;
00105                      $part1["subtype"] = "mixed";
00106                      
00107                      $part2["type"] = TYPETEXT;
00108                      $part2["subtype"] = "plain";
00109                      $part2["description"] = "imap_mail_compose() function";
00110                      $part2["contents.data"] = "message 1:xxxxxxxxxxxxxxxxxxxxxxxxxx";
00111                      
00112                      $part3["type"] = TYPETEXT;
00113                      $part3["subtype"] = "plain";
00114                      $part3["description"] = "Example";
00115                      $part3["contents.data"] = "message 2:yyyyyyyyyyyyyyyyyyyyyyyyyy";
00116                      
00117                      $part4["type"] = TYPETEXT;
00118                      $part4["subtype"] = "plain";
00119                      $part4["description"] = "Return Values";
00120                      $part4["contents.data"] = "message 3:zzzzzzzzzzzzzzzzzzzzzzzzzz";
00121                      
00122                      $body[1] = $part1;
00123                      $body[2] = $part2;
00124                      $body[3] = $part3;
00125                      $body[4] = $part4;
00126                      
00127                      $msg = imap_mail_compose($envelope, $body);
00128               }
00129        
00130               imap_append($imap_stream, $mailbox, $msg);
00131        }
00132 }
00133 
00140 function get_mailbox_name($mailbox){
00141 
00142        if (preg_match('/\{.*?\}(.*)/', $mailbox, $match) != 1) {
00143               echo "Unrecpognized mailbox name\n";
00144               return false;
00145        }
00146 
00147        return $match[1];
00148 }
00149 
00150 ?>