MailImap.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:9k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. class MailImap extends BaseObject {
  3.    var $user_name;
  4.    var $password;
  5.    var $server;
  6.    var $port;
  7.    var $connection_string;
  8.    var $folder;
  9.    var $debug;
  10.    Function MailImap( 
  11.    $user_name = '', $password = '', $server = '', $port = '') {
  12.       $this->BaseObject( 'MailImap' );
  13.       $this->user_name = '';
  14.       $this->password  = '';
  15.       $this->server    = '';
  16.       $this->port      = '';
  17.       if ( $user_name != '' ) {
  18.          $this->user_name = $user_name;
  19.       }
  20.       if ( $password != '' ) {
  21.          $this->password = $password;
  22.       }
  23.       if ( $server != '' ) {
  24.          $this->server = $server;
  25.       } else {
  26.          $this->server = 'localhost';
  27.       }
  28.       if ( $port != '' ) {
  29.          $this->port = $port;
  30.       } else {
  31.          $this->port = 143;
  32.       }
  33.       $this->connection_stream = '';
  34.       $this->connection_string = '';
  35.       $this->connected         = 0;
  36.       $this->folder = '';
  37.       $this->debug = new Debug();
  38.       $this->debug->prefix = 'MailImap';
  39.       $this->debug->Off();
  40.    }
  41.    Function CopySettings( $src_obj ) {
  42.       $this->server        = $src_obj->server;
  43.       $this->port          = $src_obj->port;
  44.       $this->user_name     = $src_obj->user_name;
  45.       $this->password      = $src_obj->password;
  46.       $this->folder        = $src_obj->folder;
  47.    }
  48.    Function DebugPrintSettings() {
  49.       $this->debug->Message( 'Server     : ' . $this->server );
  50.       $this->debug->Message( 'Port       : ' . $this->port );
  51.       $this->debug->Message( 'User Name  : ' . $this->user_name );
  52.       $this->debug->Message( 'Password   : ' . $this->password );
  53.       $this->debug->Message( 'Folder     : ' . $this->folder );
  54.       $this->debug->Message( 'Connected  : ' . $this->connected );
  55.    }
  56.    Function Connect( $folder = '' ) {
  57.       if ( $folder != '' ) {
  58.          $this->folder = $folder;
  59.       }
  60.       if ( $this->port == '' ) {
  61.          $this->port = 143;
  62.       }
  63.       if ( $this->server == '' ) {
  64.          $this->server = 'localhost';
  65.       }
  66.       if ( $this->connected == 0 ) {
  67.          /* We are not connected */
  68.          $this->connected = false;
  69.          $server = $this->server;
  70.          if ( $this->server == 'localhost' ) {
  71.             $server = '127.0.0.1';
  72.          }
  73.          $this->connection_string  = 
  74.             '{' . $server . ':' . $this->port . '}';
  75.          $this->connection_stream =
  76.             @imap_open(
  77.                $this->connection_string . $this->folder,
  78.                $this->user_name,
  79.                $this->password
  80.             );
  81.          $this->DebugPrintSettings();
  82.       }
  83.       /* Check for connection alive */
  84.       $this->connected = @imap_ping( $this->connection_stream );
  85.       if ( $this->connected ) {
  86.          return Array( true );
  87.       } else {
  88.          return Array( false, 'Invalid connection.' );
  89.       }
  90.    }
  91.    Function Disconnect() {
  92.       if ( $this->connected ) {
  93.          @imap_close( $this->connection_stream );
  94.       }
  95.       $this->connected = 0;
  96.       return Array( true );
  97.    }
  98.    Function ListFolders() {
  99.       $ret_vals = $this->Connect();
  100.       if ( $ret_vals[ 0 ] != 1 ) {
  101.           return Array( false, 'Unable to connect' );
  102.       }
  103.       $t_mailboxes = Array();
  104.       $t_mailboxes = imap_listsubscribed(
  105.          $this->connection_stream,
  106.          $this->connection_string,
  107.          '*'
  108.       );
  109.       /* Let's try to pick em up via list mailbox */
  110.       /*
  111.       $t_mailboxes = imap_listmailbox(
  112.          $this->connection_stream,
  113.          $this->connection_string,
  114.          '*'
  115.       );
  116.       */
  117.       if ( $t_mailboxes == 0 ) {
  118.          return Array();
  119.       }
  120.       $mailboxes = Array();
  121.       for( $i = 0; $i < count( $t_mailboxes ); $i++ ) {
  122.          $mailboxes[] = @imap_utf7_decode( $t_mailboxes[ $i ] );
  123.       }
  124.       return $mailboxes;
  125.    }
  126.    Function CreateFolder( $folder ) {
  127.       $ret_vals = $this->Connect();
  128.       if ( $ret_vals[ 0 ] != 1 ) {
  129.           return Array( false, 'Unable to connect' );
  130.       }
  131.       return @imap_createmailbox( 
  132.          $this->connection_stream,
  133.          $this->connection_string . @imap_utf7_encode( $folder )
  134.       );
  135.    }
  136.    Function RemoveFolder( $folder ) {
  137.       $ret_vals = $this->Connect();
  138.       if ( $ret_vals[ 0 ] != 1 ) {
  139.           return Array( false, 'Unable to connect' );
  140.       }
  141.       $this->UnSubscribeFolder( $folder );
  142.       return imap_deletemailbox(
  143.          $this->connection_stream,
  144.          $this->connection_string . @imap_utf7_encode( $folder )
  145.       );
  146.    }
  147.    Function SubscribeToFolder( $folder ) {
  148.       $ret_vals = $this->Connect();
  149.       if ( $ret_vals[ 0 ] != 1 ) {
  150.           return Array( false, 'Unable to connect' );
  151.       }
  152.       return @imap_subscribe(
  153.          $this->connection_stream,
  154.          $this->connection_string . @imap_utf7_encode( $folder )
  155.       );
  156.    }
  157.    Function UnSubscribeFolder( $folder ) {
  158.       $ret_vals = $this->Connect();
  159.       if ( $ret_vals[ 0 ] != 1 ) {
  160.           return Array( false, 'Unable to connect' );
  161.       }
  162.       return imap_unsubscribe( 
  163.          $this->connection_stream,
  164.          $this->connection_string . @imap_utf7_encode( $folder )
  165.       );
  166.    }
  167.    Function MoveMessage( $folder, $message_id ) {
  168.       $ret_vals = $this->Connect();
  169.       if ( $ret_vals[ 0 ] != 1 ) {
  170.           return Array( false, 'Unable to connect' );
  171.       }
  172.       return @imap_mail_move(
  173.          $this->connection_stream,
  174.          $message_id,
  175.          @imap_utf7_encode( $folder )
  176.       );
  177.    }
  178.    Function Folder_GetStats() {
  179.       $ret_vals = $this->Connect();
  180.       if ( $ret_vals[ 0 ] != 1 ) {
  181.           return Array( false, 'Unable to connect' );
  182.       }
  183.       $count      = @imap_num_msg( $this->connection_stream );
  184.       $recent     = @imap_num_recent( $this->connection_stream );
  185.       $new        = count(
  186.          @imap_search( $this->connection_stream, 'UNSEEN', SE_UID )
  187.       );
  188.       return Array( true, $count, $recent, $new );
  189.    }
  190.    Function Folder_Sort( 
  191.       $sort_order = SORTARRIVAL,
  192.       $reverse    = 0 ) {
  193.       $ret_vals = $this->Connect();
  194.       if ( $ret_vals[ 0 ] != 1 ) {
  195.           return Array( false, 'Unable to connect' );
  196.       }
  197.       $sorted_message_ids = Array();
  198.       $sorted_message_ids = @imap_sort(
  199.          $this->connection_stream,
  200.          $sort_order,
  201.          $reverse,
  202.          SE_UID
  203.       );
  204.       return $sorted_message_ids;
  205.    }
  206.    Function Message_GetHeader( $message_num ) {
  207.       $ret_vals = $this->Connect();
  208.       if ( $ret_vals[ 0 ] != 1 ) {
  209.           return Array( false, 'Unable to connect' );
  210.       }
  211.       $header = @imap_header(
  212.             $this->connection_stream,
  213.             @imap_msgno( $this->connection_stream, $message_num )
  214.       );
  215.       return Array( true, $header );
  216.    }
  217.    Function Message_GetBody( $message_num ) {
  218.    $ret_vals = $this->Connect();
  219.       if ( $ret_vals[ 0 ] != 1 ) {
  220.           return Array( false, 'Unable to connect' );
  221.       }
  222.       $body = @imap_body(
  223.             $this->connection_stream,
  224.             @imap_msgno( $this->connection_stream, $message_num )
  225.       );
  226.       return Array( true, $body );
  227.    }
  228.    Function Message_GetBodySection( $message_num, $section_num ) {
  229.       $ret_vals = $this->Connect();
  230.       if ( $ret_vals[ 0 ] != 1 ) {
  231.          return Array( false, 'Unable to connect' );
  232.       }
  233.       return Array( true,
  234.          @imap_fetchbody(
  235.             $this->connection_stream,
  236.             $message_num,
  237.             $section_num,
  238.             FT_UID
  239.          )
  240.       );
  241.    }
  242.    Function Message_Structure( $message_num ) {
  243.       $ret_vals = $this->Connect();
  244.       if ( $ret_vals[ 0 ] != 1 ) {
  245.          return Array( false, 'Unable to connect' );
  246.       }
  247.       return
  248.          @imap_fetchstructure(
  249.             $this->connection_stream,
  250.             @imap_msgno( $this->connection_stream, $message_num )
  251.             /* FT_UID ? */
  252.          );
  253.    }
  254.    Function Message_Delete( $message_num ) {
  255.       $ret_vals = $this->Connect();
  256.       if ( $ret_vals[ 0 ] != 1 ) {
  257.          return Array( false, 'Unable to connect' );
  258.       }
  259.       return @imap_delete( 
  260.          $this->connection_stream,
  261.          @imap_msgno( $this->connection_stream, $message_num )
  262.       );
  263.    }
  264.    Function Message_UnDelete( $message_num ) {
  265.       $ret_vals = $this->Connect();
  266.       if ( $ret_vals[ 0 ] != 1 ) {
  267.          return Array( false, 'Unable to connect' );
  268.       }
  269.       return @imap_undelete( 
  270.          $this->connection_stream,
  271.          @imap_msgno( $this->connection_stream, $message_num )
  272.       );
  273.    }
  274.    Function Message_ExpungeDeleted() {
  275.       $ret_vals = $this->Connect();
  276.       if ( $ret_vals[ 0 ] != 1 ) {
  277.          return Array( false, 'Unable to connect' );
  278.       }
  279.       return @imap_expunge( $this->connection_stream );
  280.    }
  281. }
  282. ?>