[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezutils/classes/ -> ezsmtp.php (source)

   1  <?php
   2  /***************************************
   3  ** Filename.......: class.smtp.inc
   4  ** Project........: SMTP Class
   5  ** Version........: 1.0.5
   6  ** Last Modified..: 21 December 2001
   7  ***************************************/
   8  
   9      define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
  10      define('SMTP_STATUS_CONNECTED', 2, TRUE);
  11  
  12      class smtp
  13      {
  14  
  15          var $authenticated;
  16          var $connection;
  17          var $recipients;
  18          var $CcRecipients;
  19          var $BccRecipients;
  20          var $headers;
  21          var $timeout;
  22          var $errors;
  23          var $status;
  24          var $body;
  25          var $from;
  26          var $host;
  27          var $port;
  28          var $helo;
  29          var $auth;
  30          var $user;
  31          var $pass;
  32  
  33          /***************************************
  34          ** Constructor function. Arguments:
  35          ** $params - An assoc array of parameters:
  36          **
  37          **   host    - The hostname of the smtp server        Default: localhost
  38          **   port    - The port the smtp server runs on        Default: 25
  39          **   helo    - What to send as the HELO command        Default: localhost
  40          **             (typically the hostname of the
  41          **             machine this script runs on)
  42          **   auth    - Whether to use basic authentication    Default: FALSE
  43          **   user    - Username for authentication            Default: <blank>
  44          **   pass    - Password for authentication            Default: <blank>
  45          **   timeout - The timeout in seconds for the call    Default: 5
  46          **             to fsockopen()
  47          ***************************************/
  48  
  49          function smtp( $params = array() )
  50          {
  51              if( !defined( 'CRLF' ) )
  52                  define( 'CRLF', "\r\n", TRUE );
  53  
  54              $this->authenticated = FALSE;
  55              $this->timeout       = 5;
  56              $this->status        = SMTP_STATUS_NOT_CONNECTED;
  57              $this->host          = 'localhost';
  58              $this->port          = 25;
  59              $this->helo          = 'localhost';
  60              $this->auth          = FALSE;
  61              $this->user          = '';
  62              $this->pass          = '';
  63              $this->errors        = array();
  64  
  65              foreach ( $params as $key => $value )
  66              {
  67                  $this->$key = $value;
  68              }
  69          }
  70  
  71          /***************************************
  72          ** Connect function. This will, when called
  73          ** statically, create a new smtp object,
  74          ** call the connect function (ie this function)
  75          ** and return it. When not called statically,
  76          ** it will connect to the server and send
  77          ** the HELO command.
  78          ***************************************/
  79  
  80          function connect($params = array())
  81          {
  82              if ( !isset( $this->status ) )
  83              {
  84                  $obj = new smtp( $params );
  85                  if( $obj->connect() )
  86                  {
  87                      $obj->status = SMTP_STATUS_CONNECTED;
  88                  }
  89                  return $obj;
  90              }
  91              else
  92              {
  93                  $this->connection = fsockopen( $this->host, $this->port, $errno, $errstr, $this->timeout );
  94                  if ( function_exists( 'socket_set_timeout' ) )
  95                  {
  96                      @socket_set_timeout( $this->connection, 5, 0 );
  97                  }
  98  
  99                  $greeting = $this->get_data();
 100                  if ( is_resource( $this->connection ) )
 101                  {
 102                      return $this->auth ? $this->ehlo() : $this->helo();
 103                  }
 104                  else
 105                  {
 106                      $this->errors[] = 'Failed to connect to server: ' . $errstr;
 107                      return FALSE;
 108                  }
 109              }
 110          }
 111  
 112          /***************************************
 113          ** Function which handles sending the mail.
 114          ** Arguments:
 115          ** $params    - Optional assoc array of parameters.
 116          **            Can contain:
 117          **              recipients - Indexed array of recipients
 118          **              from       - The from address. (used in MAIL FROM:),
 119          **                           this will be the return path
 120          **              headers    - Indexed array of headers, one header per array entry
 121          **              body       - The body of the email
 122          **            It can also contain any of the parameters from the connect()
 123          **            function
 124          ***************************************/
 125  
 126          function send( $params = array() )
 127          {
 128              foreach ( $params as $key => $value )
 129              {
 130                  $this->set( $key, $value );
 131              }
 132  
 133              if ( $this->is_connected() )
 134              {
 135                  // Do we auth or not? Note the distinction between the auth variable and auth() function
 136                  if ( $this->auth AND !$this->authenticated )
 137                  {
 138                      if ( !$this->auth() )
 139                          return FALSE;
 140                  }
 141                  $this->mail( $this->from );
 142                  if ( is_array( $this->recipients ) )
 143                      foreach ( $this->recipients as $value )
 144                          $this->rcpt( $value );
 145                  else
 146                      $this->rcpt( $this->recipients );
 147  
 148                  if ( is_array( $this->CcRecipients ) )
 149                      foreach( $this->CcRecipients as $value )
 150                          $this->rcpt( $value );
 151                  else
 152                      $this->rcpt( $this->CcRecipients );
 153  
 154                  if ( is_array( $this->BccRecipients ) )
 155                      foreach ( $this->BccRecipients as $value )
 156                          $this->rcpt( $value );
 157                  else
 158                      $this->rcpt( $this->BccRecipients );
 159  
 160                  if ( !$this->data() )
 161                      return FALSE;
 162  
 163                  // Transparency
 164                  $headers = str_replace( CRLF.'.', CRLF.'..', trim( implode( CRLF, $this->headers ) ) );
 165                  $body    = str_replace( CRLF.'.', CRLF.'..', $this->body );
 166                  $body    = $body[0] == '.' ? '.'.$body : $body;
 167  
 168                  $this->send_data( $headers );
 169                  $this->send_data( '' );
 170                  $this->send_data( $body );
 171                  $this->send_data( '.' );
 172  
 173                  $result = ( substr( trim( $this->get_data() ), 0, 3) === '250' );
 174                  return $result;
 175              }
 176              else
 177              {
 178                  $this->errors[] = 'Not connected!';
 179                  return FALSE;
 180              }
 181          }
 182  
 183          /***************************************
 184          ** Function to implement HELO cmd
 185          ***************************************/
 186  
 187          function helo()
 188          {
 189              $error = $this->get_data();
 190              if ( is_resource( $this->connection ) and
 191                   $this->send_data( 'HELO ' . $this->helo ) and
 192                   substr( trim( $error ), 0, 3 ) === '250' )
 193              {
 194                  return TRUE;
 195              }
 196              else
 197              {
 198                  $this->errors[] = 'HELO command failed, output: ' . trim( substr( trim( $error ), 3 ) );
 199                  return FALSE;
 200              }
 201          }
 202  
 203          /***************************************
 204          ** Function to implement EHLO cmd
 205          ***************************************/
 206  
 207          function ehlo()
 208          {
 209              $error = $this->get_data();
 210              if ( is_resource( $this->connection ) and
 211                   $this->send_data( 'EHLO ' . $this->helo ) and
 212                   substr( trim( ), 0, 3 ) === '250' )
 213              {
 214                  return TRUE;
 215              }
 216              else
 217              {
 218                  $this->errors[] = 'EHLO command failed, output: ' . trim( substr( trim( $error ), 3 ) );
 219                  return FALSE;
 220              }
 221          }
 222  
 223          /***************************************
 224          ** Function to implement RSET cmd
 225          ***************************************/
 226  
 227          function rset()
 228          {
 229              $error = $this->get_data();
 230              if ( is_resource( $this->connection ) and
 231                   $this->send_data( 'RSET' ) and
 232                   substr( trim( $error ), 0, 3 ) === '250' )
 233              {
 234                  return TRUE;
 235              }
 236              else
 237              {
 238                  $this->errors[] = 'RSET command failed, output: ' . trim( substr( trim( $error ), 3 ) );
 239                  return FALSE;
 240              }
 241          }
 242  
 243          /***************************************
 244          ** Function to implement QUIT cmd
 245          ***************************************/
 246  
 247          function quit()
 248          {
 249              $error = $this->get_data();
 250              if ( is_resource( $this->connection ) and
 251                   $this->send_data( 'QUIT' ) and
 252                   substr( trim( $error ), 0, 3 ) === '221' )
 253              {
 254                  fclose( $this->connection );
 255                  $this->status = SMTP_STATUS_NOT_CONNECTED;
 256                  return TRUE;
 257              }
 258              else
 259              {
 260                  $this->errors[] = 'QUIT command failed, output: ' . trim( substr( trim( $error ), 3 ) );
 261                  return FALSE;
 262              }
 263          }
 264  
 265          /***************************************
 266          ** Function to implement AUTH cmd
 267          ***************************************/
 268  
 269          function auth()
 270          {
 271              $error = $this->get_data();
 272              if ( is_resource( $this->connection ) and
 273                   $this->send_data( 'AUTH LOGIN' ) and
 274                   substr( trim( $error ), 0, 3) === '334' and
 275                   $this->send_data( base64_encode( $this->user ) ) and // Send username
 276                   substr( trim( $error ), 0, 3 ) === '334' and
 277                   $this->send_data( base64_encode( $this->pass ) ) and // Send password
 278                   substr( trim( $error ), 0, 3 ) === '235' )
 279              {
 280                  $this->authenticated = TRUE;
 281                  return TRUE;
 282              }
 283              else
 284              {
 285                  $this->errors[] = 'AUTH command failed: ' . trim( substr( trim( $error ), 3 ) );
 286                  return FALSE;
 287              }
 288          }
 289  
 290          /***************************************
 291          ** Function that handles the MAIL FROM: cmd
 292          ***************************************/
 293  
 294          function mail( $from )
 295          {
 296              if ( !preg_match( "/<.+>/", $from ) )
 297                  $from = '<' . $from .'>';
 298              if ( $this->is_connected() and
 299                   $this->send_data( 'MAIL FROM:' . $from . '' ) and
 300                   substr( trim( $this->get_data() ), 0, 3 ) === '250' )
 301              {
 302                  return TRUE;
 303              }
 304              else
 305              {
 306                  return FALSE;
 307              }
 308          }
 309  
 310          /***************************************
 311          ** Function that handles the RCPT TO: cmd
 312          ***************************************/
 313  
 314          function rcpt( $to )
 315          {
 316              if ( !preg_match( "/<.+>/", $to ) )
 317                  $to = '<' . $to .'>';
 318  
 319              $error = $this->get_data();
 320              if ( $this->is_connected() and
 321                   $this->send_data( 'RCPT TO:' . $to . '' ) and
 322                   substr( trim( $error ), 0, 3 ) === '250' )
 323              {
 324                  return TRUE;
 325              }
 326              else
 327              {
 328                  $this->errors[] = trim( substr( trim( $error ), 3 ) );
 329                  return FALSE;
 330              }
 331          }
 332  
 333          /***************************************
 334          ** Function that sends the DATA cmd
 335          ***************************************/
 336  
 337          function data()
 338          {
 339              $error = $this->get_data();
 340              if( $this->is_connected() and
 341                  $this->send_data( 'DATA' ) and
 342                  substr( trim( $error ), 0, 3 ) === '354' )
 343              {
 344                  return TRUE;
 345              }
 346              else
 347              {
 348                  $this->errors[] = trim( substr( trim( $error ), 3 ) );
 349                  return FALSE;
 350              }
 351          }
 352  
 353          /***************************************
 354          ** Function to determine if this object
 355          ** is connected to the server or not.
 356          ***************************************/
 357  
 358          function is_connected()
 359          {
 360              return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
 361          }
 362  
 363          /***************************************
 364          ** Function to send a bit of data
 365          ***************************************/
 366  
 367          function send_data( $data )
 368          {
 369              if ( is_resource( $this->connection ) )
 370              {
 371                  return fwrite( $this->connection, $data.CRLF, strlen( $data ) + 2 );
 372              }
 373              else
 374                  return FALSE;
 375          }
 376  
 377          /***************************************
 378          ** Function to get data.
 379          ***************************************/
 380  
 381          function get_data()
 382          {
 383              $return = '';
 384              $line   = '';
 385              $loops  = 0;
 386  
 387              if ( is_resource( $this->connection ) )
 388              {
 389                  while ( ( strpos( $return, CRLF ) === FALSE OR substr( $line, 3, 1 ) !== ' ' ) AND $loops < 100 )
 390                  {
 391                      $line    = fgets( $this->connection, 512 );
 392                      $return .= $line;
 393                      $loops++;
 394                  }
 395                  return $return;
 396              }
 397              else
 398                  return FALSE;
 399          }
 400  
 401          /***************************************
 402          ** Sets a variable
 403          ***************************************/
 404  
 405          function set( $var, $value )
 406          {
 407              $this->$var = $value;
 408              return TRUE;
 409          }
 410  
 411      } // End of class
 412  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7