1. <?
  2. #########################
  3. #1. TEIL zb. smtp.php                 #
  4. #########################
  5. /*
  6. include(\"smtp.class.php\");
  7.  
  8.  
  9. $smtp = new SMTP( $server, $user_name, $user_pass );
  10. $error = $smtp->send( array( $HTTP_POST_VARS['target'] ),
  11.                        $HTTP_POST_VARS['subject'],
  12.                        $HTTP_POST_VARS['text'] );
  13. if($error)echo$error;
  14.  
  15. */
  16. #########################
  17. #2. Teil:     smtp.class.php          #
  18. #########################
  19.  
  20. /**
  21.  * class:         smtp.class.php
  22.  * require:
  23.  * optional:    
  24.  * description:    class for sending mail directly throw an smtp server
  25.  * created:        25.08.2002
  26.  * last change:    18.09.2002
  27.  * author:        Sven Denkert <sven.denkert@t-online.de>
  28.  * copyright:    Sven Denkert
  29.  * 
  30.  * TODO:        MIME / Attachments / error-handling like pop3.class.php
  31.  */
  32.  
  33. if ( !isset( $_CLASS_SMTP_ ) ) {
  34.     $_CLASS_SMTP_ = 1;
  35.     class SMTP {
  36.         var $server = &#092;"\";
  37.         var $user_name = &#092;"\";
  38.         var $user_pass = &#092;"\";
  39.         var $from = &#092;"ja@wp.pl\";
  40.  
  41.         var $socket;
  42.         var $error;
  43.  
  44.         function SMTP( $server, $user_name = null, $user_pass = null )
  45.         {
  46.             if ( !$this->server = $server )
  47.                 return $this->error = &#092;"No SMTP server provided\";
  48.             if ( trim( $user_name ) )
  49.                 $this->user_name = trim( $user_name );
  50.             if ( trim( $user_pass ) )
  51.                 $this->user_pass = trim( $user_pass );
  52.         } 
  53.  
  54.         function &server_parse( $response )
  55.         {
  56.             $server_response = &#092;"\";
  57.             while ( substr( $server_response, 3, 1 ) != ' ' )
  58.             if ( !( $server_response = fgets( $this->socket, 256 ) ) )
  59.                 return $this->error = &#092;"Couldn't get mail server response codes\";
  60.             if ( substr( $server_response, 0, 3 ) != $response )
  61.                 return $this->error = &#092;"Couldn't not send mail. Server response: $server_response\";
  62.             return &#092;"\";
  63.         } 
  64.  
  65.         function &put( $string )
  66.         {
  67.             return fputs( $this->socket, $string . &#092;"rn\" );
  68.         } 
  69.         // put in
  70.         // mail_to => array of mailadresses, or string splittet with ,
  71.         // cc => array of mailadresses, or string splittet with ,
  72.         // cc => array of mailadresses, or string splittet with ,
  73.         function &send( $mail_to, $subject, $message, $cc = &#092;"\", $bcc = \"\" )
  74.         { 
  75.             // Fix any bare linefeeds in the message to make it RFC821 Compliant.
  76.            // $message = preg_replace( \"/(?<!r)n/si\", \"rn\", $message ); 
  77.             if ( !trim( $subject ) ) return $this->error = &#092;"No email Subject specified\";
  78.             if ( !trim( $message ) ) return $this->error = &#092;"Email message was blank\"; 
  79.             if ( !is_array( $mail_to ) ) $mail_to = explode( &#092;",\", $mail_to );
  80.             if ( $cc && !is_array( $cc ) ) $cc = explode( &#092;",\", $cc );
  81.             if ( $bcc && !is_array( $bcc ) ) $bcc = explode( &#092;",\", $bcc ); 
  82.             if ( !$this->socket = fsockopen( $this->server, 25, $errno, $errstr, 20 ) )
  83.                 return $this->error = &#092;"Could not connect to smtp host : $errno : $errstr\";
  84.             if ( $this->server_parse( &#092;"220\" ) )
  85.                 return $this->error; 
  86.             if ( !$this->put( &#092;"EHLO \" . $this->server ) )
  87.                 return $this->error = &#092;"cannot send EHLO Command\";
  88.             if ( $this->server_parse( &#092;"250\" ) )
  89.                 return $this->error; 
  90.             if ( !empty( $this->user_name ) && !empty( $this->user_pass ) ) {
  91.                 if ( !$this->put( &#092;"AUTH LOGIN\" ) )
  92.                     return $this->error = &#092;"Cannot send AUTH LOGIN Command\";
  93.                 if ( $this->server_parse( &#092;"334\" ) )
  94.                     return $this->error;
  95.                 if ( !$this->put( base64_encode( $this->user_name ) ) )
  96.                     return $this->error = &#092;"Cannot send LOGIN USER\";
  97.                 if ( $this->server_parse( &#092;"334\" ) )
  98.                     return $this->error;
  99.                 if ( !$this->put( base64_encode( $this->user_pass ) ) )
  100.                     return $this->error = &#092;"Cannot send USER PASS\";
  101.                 if ( $this->server_parse( &#092;"235\" ) )
  102.                     return $this->error;
  103.             } 
  104.             if ( !$this->put( &#092;"MAIL FROM: \" . $this->from ) )
  105.                 return $this->error = &#092;"Cannot send MAIL FROM\";
  106.             if ( $this->server_parse( &#092;"250\" ) )
  107.                 return $this->error; 
  108.             
  109.                   $to_header = &#092;"FROM: <\" . $this->from . \"> rn\";
  110.             $to_header .= &#092;"To: \";
  111.  
  112.  
  113.             @reset( $mail_to );
  114.             while ( list( , $mail_to_address ) = each( $mail_to ) ) {
  115.                 $mail_to_address = trim( $mail_to_address );
  116.                 if ( preg_match( '/.+@.+/', $mail_to_address ) ) {
  117.                     fputs( $this->socket, &#092;"RCPT TO: $mail_to_addressrn\" );
  118.                     $this->server_parse( &#092;"250\" );
  119.                 } 
  120.                 $to_header .= &#092;"$mail_to_address, \";
  121.             } 
  122.             if ( !empty( $bcc ) ) {
  123.                 @reset( $bcc );
  124.                 while ( list( , $bcc_address ) = each( $bcc ) ) {
  125.                     $bcc_address = trim( $bcc_address );
  126.                     if ( preg_match( '/.+@.+/', $bcc_address ) ) {
  127.                         fputs( $this->socket, &#092;"RCPT TO: $bcc_addressrn\" );
  128.                         $this->server_parse( &#092;"250\" );
  129.                     } 
  130.                 } 
  131.             } 
  132.             if ( !empty( $cc ) ) {
  133.                 @reset( $cc );
  134.                 while ( list( , $cc_address ) = each( $cc ) ) {
  135.                     $cc_address = trim( $cc_address );
  136.                     if ( preg_match( '/.+@.+/', $cc_address ) ) {
  137.                         fputs( $this->socket, &#092;"RCPT TO: $cc_addressrn\" );
  138.                         $this->server_parse( &#092;"250\" );
  139.                     } 
  140.                 } 
  141.                 $to_header .= &#092;"$cc_address,\";
  142.             } 
  143.             fputs( $this->socket, &#092;"DATArn\" ); 
  144.             $this->server_parse( &#092;"354\" ); 
  145.             fputs( $this->socket, &#092;"Subject: $subjectrn\" ); 
  146.             fputs( $this->socket, &#092;"$to_headerrn\" ); 
  147.             fputs( $this->socket, &#092;"rnrn\" ); 
  148.             fputs( $this->socket, &#092;"$messagern\" ); 
  149.             fputs( $this->socket, &#092;".rn\" );
  150.             $this->server_parse( &#092;"250\" ); 
  151.             fputs( $this->socket, &#092;"QUITrn\" );
  152.             fclose( $this->socket );
  153.             $this->socket = null;
  154.         } 
  155.     } 
  156. }
  157. ?>


i nie wiem jak dodac to tego skryptu naglowek aby mozna bylo wysylac e-miale w formacie html. worriedsmiley.gif