MailMessage.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:12k
- <?php
- class MailMessage extends BaseObject {
- var $from;
- var $to;
- var $cc;
- var $bcc;
- var $subject;
- var $headers;
- var $message_body;
- var $attatchments;
- var $attatchments_filename_override;
- var $attatchments_comments;
- var $sendmail;
- var $sendmail_command;
- var $smtp;
- var $smtp_server;
- var $smtp_port;
- var $mime_boundary;
- var $new_line;
- var $net_object;
- var $debug;
- var $fp;
- var $charset;
- Function MailMessage() {
- $this->BaseObject( 'MailMessage' );
- $this->from = '';
- $this->to = '';
- $this->subject = '';
- $this->message_body = '';
- $this->headers = Array();
- $this->attatchments = Array();
- $this->attatchments_comments = Array();
- $this->attatchments_filename_override = Array();
- $this->sendmail = 0;
- $this->sendmail_command = 'sendmail';
- $this->smtp = 1;
- $this->smtp_server = 'localhost';
- $this->smtp_port = 25;
- $this->net_obj = new Net();
- $this->new_line = "rn";
- $this->debug = new Debug();
- $this->debug->On();
- $this->fp = undef;
- $this->charset = 'US-ASCII';
- }
- Function AddFile( $path_to_file, $comment = '', $fname_override = '') {
- return $this->AttatchFile( $path_to_file, $comment, $fname_override );
- }
- Function AttatchFile( $path_to_file, $comment = '', $fname_override = '' ) {
- if ( file_exists( $path_to_file ) ) {
- $this->attatchments[] = $path_to_file;
- $this->attatchments_comments[ $path_to_file ] = $comment;
- $this->attatchments_filename_override[ $path_to_file ] =
- $fname_override;
- return 1;
- }
- return 0;
- }
- Function AddHeader( $header, $value ) {
- $this->headers[ $header ] = $value;
- }
- Function Send() {
- if ( $this->sendmail == 1 ) {
- $this->debug->Message( 'Send using sendmail' );
- return $this->SendUsingSendmail();
- }
- if ( $this->smtp == 1 ) {
- $this->debug->Message( 'Send using smtp' );
- return $this->SendUsingSMTP();
- }
- return 0;
- }
- Function ExplodeAddressList( $address_list ) {
- if ( $address_list == '' ) {
- return Array();
- }
- return split( ',', $address_list );
- }
- Function StripMailAddressBare( $address ) {
- $bare_addr = $address;
- /* make sure the mail from is in the format of user@host.com */
- if ( ereg( '<(.*)>', $address , $regs ) ) {
- $bare_addr = $regs[ 1 ];
- }
- return $bare_addr;
- }
- Function FpPrint( $line, $new_line = 1 ) {
- $this->debug->Message( 'SEND: ' . $line );
- if ( $this->smtp == 1 ) {
- $this->net_obj->SendLine( $line );
- } else {
- fputs( $this->fp, $line );
- }
- if ( $new_line == 1 && $this->smtp == 1 ) {
- $this->net_obj->SendLine( $this->new_line );
- } else if ( $new_line == 1 ) {
- fputs( $this->fp, $this->new_line );
- }
- }
- Function Write_RFC822_Headers() {
- /* Date header isn't really neaded */
- // date = date( 'D, j M Y H:i:s ", mktime()) . timezone();
- $this->FpPrint( 'Subject: ' . $this->subject );
- $this->FpPrint( 'From: ' . $this->from );
- $this->FpPrint( 'To: ' . $this->to );
- if ( $this->cc != undef && $this->cc != '' ) {
- $this->FpPrint( 'Cc: ' . $this->cc );
- }
- if ( $this->bcc != undef && $this->bcc != '' ) {
- $this->FpPrint( 'Bcc: ' . $this->bcc );
- }
- // Write user headers
- reset( $this->headers );
- while( list( $header_name, $header_value ) = each( $this->headers ) ) {
- $this->FpPrint( $header_name . ': ' . $header_value );
- }
- // Write MIME headers
- $this->FpPrint( 'MIME-Version: 1.0' );
- if ( count( $this->attatchments ) > 0 ) {
- // Generate a mime boundary
- $this->mime_boundary =
- '0-' . microtime() . '-912837412-' . time() . '=:4553';
- $this->mime_boundary = str_replace( ' ', '', $this->mime_boundary );
- $this->FpPrint(
- 'Content-Type: multipart/mixed; BOUNDARY="' .
- $this->mime_boundary . '"'
- );
- // dummy error message
- $this->FpPrint(
- ' This message is in MIME format. The first part should be ' . $this->new_line .
- ' readable text, while the remaining parts are likely' . $this->new_line .
- ' unreadable without MIME-aware tools. Send mail to' . $this->new_line .
- ' mime@docserver.cac.washington.edu for more info.' . $this->new_line
- );
- $this->FpPrint( '--' . $this->mime_boundary );
- $this->FpPrint( 'Content-Type: text/plain; charset=' . $this->charset );
- $this->FpPrint( '' );
- } else {
- $this->FpPrint( 'Content-Type: text/plain; charset=' . $this->charset );
- }
- $this->FpPrint( '' );
- }
- Function Write_MessageBody() {
- $this->FpPrint( $this->message_body );
- // attatchments are mime encoded
- if ( count( $this->attatchments ) > 0 ) {
- $this->FpPrint( '' );
- $mime_util = new MimeObject();
- // Attatch the seperate files
- for( $x = 0; $x < count( $this->attatchments ); $x++ ) {
- $current_file = $this->attatchments[ $x ];
- if ( file_exists( $current_file ) ) {
- $fh = fopen( $current_file, 'r' ) ;
- $file_contents = fread( $fh, filesize( $current_file ) );
- fclose( $fh );
- $this->FpPrint( '--' . $this->mime_boundary );
- $cur_file = $current_file;
- if ( $this->attatchments_filename_override[ $current_file ] != '' ) {
- $cur_file = $this->attatchments_filename_override[ $current_file ];
- }
- $this->FpPrint(
- 'Content-Type: ' .
- $mime_util->GetType( $cur_file ) . ';' .
- 'name="' . basename( $cur_file ) . '"'
- );
- $this->FpPrint( 'Content-Transfer-Encoding: base64' );
- /* TODO Fix the content description handlers */
- $this->FpPrint(
- 'Content-Description: ' .
- $this->attatchments_comments[ $current_file ]
- );
- $this->FpPrint( '' );
- /* Output the mimeencode results */
- $this->FpPrint( chunk_split( base64_encode( $file_contents ) ), 0 );
- }
- } /* End for attatchments */
- }
- }
- Function SMTP_ErrorCheck( $line ) {
- list( $error_number, $message ) = split( ' ', $line );
- switch( $error_number ) {
- case 211: /* System status, or system help reply */
- case 214: /* Help message */
- case 220: /* Service ready */
- case 221: /* Service closing transmission channel */
- case 250: /* Requested mail action okay, completed */
- case 251: /* User not local; will forward */
- case 354: /* Start mail input; end with . */
- return 1;
- break;
- case 421: /* Service not avaiable, closing channel */
- case 450: /* Requested mail action not taken: mailbox unavaiable */
- case 451: /* Requested action aborted: error in processing */
- case 452: /* Requested action not taken: insufficient system storage */
- case 500: /* Syntax error; command not recognized */
- case 501: /* Syntax error in paramaters or arguments */
- case 502: /* Command not implemented */
- case 503: /* Bad sequence of commands */
- case 504: /* Command parameter not implemented */
- case 550: /* Requested action not taken: mailbox unavailable */
- case 551: /* User not local; please try forwarding */
- case 552: /* Requested mail action aborted: exceeding storage allocation */
- case 553: /* Requested action not taken: mailbox name not allowed */
- case 554: /* Transaction failed */
- return 0;
- break;
- default: /* Unknown response line */
- return 0;
- break;
- }
- }
- Function SMTP_ErrorTrap() {
- $temp_line = $this->net_obj->ReadLine();
- $this->debug->Message( 'RECV: ' . $temp_line );
- if ( ! $this->SMTP_ErrorCheck( $temp_line ) ) {
- return 0;
- }
- return 1;
- }
- Function SendUsingSMTP() {
- $this->net_obj->server = $this->smtp_server;
- $this->net_obj->port = $this->smtp_port;
- $this->net_obj->Connect();
- if ( $this->net_obj->connected == 0 ) {
- return 0;
- }
- $mail_from = $this->StripMailAddressBare( $this->from );
- list( $username, $domain ) = split( '@', $mail_from );
- $mail_to_arr = Array();
- $mail_cc_arr = Array();
- $mail_bcc_arr = Array();
- $mail_to_arr = $this->ExplodeAddressList( $this->to );
- $mail_cc_arr = $this->ExplodeAddressList( $this->cc );
- $mail_bcc_arr = $this->ExplodeAddressList( $this->bcc );
- /* Check for a vaild helo open for buisness */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- /* Send a helo domain.com */
- $this->FpPrint( 'HELO ' . $domain );
- /* Check for a vaild helo open for buisness */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- /* Send who is sending the message */
- $this->FpPrint( 'MAIL FROM<' . $mail_from . '>' );
- /* Was that okay with the server? */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- /* To */
- for( $i = 0; $i < count( $mail_to_arr ); $i++ ) {
- /* Send a rcpt to address */
- $this->debug->Message( $mail_to_arr[ $i ] );
- $this->FpPrint(
- 'RCPT TO:<' .
- $this->StripMailAddressBare( $mail_to_arr[ $i ] ) .
- '>'
- );
- /* Was that okay with the server? */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- }
- /* Cc */
- for( $i = 0; $i < count( $mail_cc_arr ); $i++ ) {
- /* Send a rcpt to address */
- $this->debug->Message( $mail_cc_arr[ $i ] );
- $this->FpPrint(
- 'RCPT TO:<' .
- $this->StripMailAddressBare( $mail_cc_arr[ $i ] ) .
- '>'
- );
- /* Was that okay with the server? */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- }
- /* Bcc */
- for( $i = 0; $i < count( $mail_bcc_arr ); $i++ ) {
- /* Send a rcpt to address */
- $this->debug->Message( $mail_bcc_arr[ $i ] );
- $this->FpPrint(
- 'RCPT TO:<' .
- $this->StripMailAddressBare( $mail_bcc_arr[ $i ] ) .
- '>'
- );
- /* Was that okay with the server? */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- }
- $this->FpPrint( 'DATA' );
- /* Was that okay with the server? */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- $this->Write_RFC822_Headers( $this->net_obj->connection_handle );
- $this->Write_MessageBody( $this->net_obj->connection_handle );
- $this->FpPrint( '.' );
- /* Was that okay with the server? */
- if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- $this->FpPrint( 'QUIT' );
- /* Was that okay with the server? */
- // if ( ! $this->SMTP_ErrorTrap() ) { return 0; }
- $this->net_obj->Disconnect();
- return 1;
- }
- Function SendUsingSendmail() {
- $this->fp = 0;
- $sendmail_command = Array(
- $this->sendmail_command,
- '-i'
- );
- if ( $this->from != '' ) {
- $mail_from = $this->StripMailAddressBare( $this->from );
- $sendmail_command[] = '-i -f "' . $mail_from . '"';
- }
- $sendmail_command[] = '"' . $this->to . '"';
- $this->fp = popen( join( ' ', $sendmail_command ), 'w');
- /* We failed to open the process */
- if ( $this->fp == 0 ) { return 0; }
- $this->Write_RFC822_Headers();
- $this->Write_MessageBody();
- pclose( $this->fp );
- return 1;
- }
- }
- ?>