MailImap.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:9k
- <?php
- class MailImap extends BaseObject {
- var $user_name;
- var $password;
- var $server;
- var $port;
- var $connection_string;
- var $folder;
- var $debug;
- Function MailImap(
- $user_name = '', $password = '', $server = '', $port = '') {
- $this->BaseObject( 'MailImap' );
- $this->user_name = '';
- $this->password = '';
- $this->server = '';
- $this->port = '';
- if ( $user_name != '' ) {
- $this->user_name = $user_name;
- }
- if ( $password != '' ) {
- $this->password = $password;
- }
- if ( $server != '' ) {
- $this->server = $server;
- } else {
- $this->server = 'localhost';
- }
- if ( $port != '' ) {
- $this->port = $port;
- } else {
- $this->port = 143;
- }
- $this->connection_stream = '';
- $this->connection_string = '';
- $this->connected = 0;
- $this->folder = '';
- $this->debug = new Debug();
- $this->debug->prefix = 'MailImap';
- $this->debug->Off();
- }
- Function CopySettings( $src_obj ) {
- $this->server = $src_obj->server;
- $this->port = $src_obj->port;
- $this->user_name = $src_obj->user_name;
- $this->password = $src_obj->password;
- $this->folder = $src_obj->folder;
- }
- Function DebugPrintSettings() {
- $this->debug->Message( 'Server : ' . $this->server );
- $this->debug->Message( 'Port : ' . $this->port );
- $this->debug->Message( 'User Name : ' . $this->user_name );
- $this->debug->Message( 'Password : ' . $this->password );
- $this->debug->Message( 'Folder : ' . $this->folder );
- $this->debug->Message( 'Connected : ' . $this->connected );
- }
- Function Connect( $folder = '' ) {
- if ( $folder != '' ) {
- $this->folder = $folder;
- }
- if ( $this->port == '' ) {
- $this->port = 143;
- }
- if ( $this->server == '' ) {
- $this->server = 'localhost';
- }
- if ( $this->connected == 0 ) {
- /* We are not connected */
- $this->connected = false;
- $server = $this->server;
- if ( $this->server == 'localhost' ) {
- $server = '127.0.0.1';
- }
- $this->connection_string =
- '{' . $server . ':' . $this->port . '}';
- $this->connection_stream =
- @imap_open(
- $this->connection_string . $this->folder,
- $this->user_name,
- $this->password
- );
- $this->DebugPrintSettings();
- }
- /* Check for connection alive */
- $this->connected = @imap_ping( $this->connection_stream );
- if ( $this->connected ) {
- return Array( true );
- } else {
- return Array( false, 'Invalid connection.' );
- }
- }
- Function Disconnect() {
- if ( $this->connected ) {
- @imap_close( $this->connection_stream );
- }
- $this->connected = 0;
- return Array( true );
- }
- Function ListFolders() {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- $t_mailboxes = Array();
- $t_mailboxes = imap_listsubscribed(
- $this->connection_stream,
- $this->connection_string,
- '*'
- );
- /* Let's try to pick em up via list mailbox */
- /*
- $t_mailboxes = imap_listmailbox(
- $this->connection_stream,
- $this->connection_string,
- '*'
- );
- */
- if ( $t_mailboxes == 0 ) {
- return Array();
- }
- $mailboxes = Array();
- for( $i = 0; $i < count( $t_mailboxes ); $i++ ) {
- $mailboxes[] = @imap_utf7_decode( $t_mailboxes[ $i ] );
- }
- return $mailboxes;
- }
- Function CreateFolder( $folder ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return @imap_createmailbox(
- $this->connection_stream,
- $this->connection_string . @imap_utf7_encode( $folder )
- );
- }
- Function RemoveFolder( $folder ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- $this->UnSubscribeFolder( $folder );
- return imap_deletemailbox(
- $this->connection_stream,
- $this->connection_string . @imap_utf7_encode( $folder )
- );
- }
- Function SubscribeToFolder( $folder ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return @imap_subscribe(
- $this->connection_stream,
- $this->connection_string . @imap_utf7_encode( $folder )
- );
- }
- Function UnSubscribeFolder( $folder ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return imap_unsubscribe(
- $this->connection_stream,
- $this->connection_string . @imap_utf7_encode( $folder )
- );
- }
- Function MoveMessage( $folder, $message_id ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return @imap_mail_move(
- $this->connection_stream,
- $message_id,
- @imap_utf7_encode( $folder )
- );
- }
- Function Folder_GetStats() {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- $count = @imap_num_msg( $this->connection_stream );
- $recent = @imap_num_recent( $this->connection_stream );
- $new = count(
- @imap_search( $this->connection_stream, 'UNSEEN', SE_UID )
- );
- return Array( true, $count, $recent, $new );
- }
- Function Folder_Sort(
- $sort_order = SORTARRIVAL,
- $reverse = 0 ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- $sorted_message_ids = Array();
- $sorted_message_ids = @imap_sort(
- $this->connection_stream,
- $sort_order,
- $reverse,
- SE_UID
- );
- return $sorted_message_ids;
- }
- Function Message_GetHeader( $message_num ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- $header = @imap_header(
- $this->connection_stream,
- @imap_msgno( $this->connection_stream, $message_num )
- );
- return Array( true, $header );
- }
- Function Message_GetBody( $message_num ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- $body = @imap_body(
- $this->connection_stream,
- @imap_msgno( $this->connection_stream, $message_num )
- );
- return Array( true, $body );
- }
- Function Message_GetBodySection( $message_num, $section_num ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return Array( true,
- @imap_fetchbody(
- $this->connection_stream,
- $message_num,
- $section_num,
- FT_UID
- )
- );
- }
- Function Message_Structure( $message_num ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return
- @imap_fetchstructure(
- $this->connection_stream,
- @imap_msgno( $this->connection_stream, $message_num )
- /* FT_UID ? */
- );
- }
- Function Message_Delete( $message_num ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return @imap_delete(
- $this->connection_stream,
- @imap_msgno( $this->connection_stream, $message_num )
- );
- }
- Function Message_UnDelete( $message_num ) {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return @imap_undelete(
- $this->connection_stream,
- @imap_msgno( $this->connection_stream, $message_num )
- );
- }
- Function Message_ExpungeDeleted() {
- $ret_vals = $this->Connect();
- if ( $ret_vals[ 0 ] != 1 ) {
- return Array( false, 'Unable to connect' );
- }
- return @imap_expunge( $this->connection_stream );
- }
- }
- ?>