[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/ -> class.mailer-smtp.php (source)

   1  <?php
   2  // Last sync [WP13425]
   3  
   4  /*~ class.smtp.php
   5  .---------------------------------------------------------------------------.
   6  |  Software: PHPMailer - PHP email class                                    |
   7  |   Version: 2.0.4                                                          |
   8  |   Contact: via sourceforge.net support pages (also www.codeworxtech.com)  |
   9  |      Info: http://phpmailer.sourceforge.net                               |
  10  |   Support: http://sourceforge.net/projects/phpmailer/                     |
  11  | ------------------------------------------------------------------------- |
  12  |    Author: Andy Prevost (project admininistrator)                         |
  13  |    Author: Brent R. Matzelle (original founder)                           |
  14  | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved.               |
  15  | Copyright (c) 2001-2003, Brent R. Matzelle                                |
  16  | ------------------------------------------------------------------------- |
  17  |   License: Distributed under the Lesser General Public License (LGPL)     |
  18  |            http://www.gnu.org/copyleft/lesser.html                        |
  19  | This program is distributed in the hope that it will be useful - WITHOUT  |
  20  | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
  21  | FITNESS FOR A PARTICULAR PURPOSE.                                         |
  22  | ------------------------------------------------------------------------- |
  23  | We offer a number of paid services (www.codeworxtech.com):                |
  24  | - Web Hosting on highly optimized fast and secure servers                 |
  25  | - Technology Consulting                                                   |
  26  | - Oursourcing (highly qualified programmers and graphic designers)        |
  27  '---------------------------------------------------------------------------'
  28   */
  29  /**
  30   * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
  31   * commands except TURN which will always return a not implemented
  32   * error. SMTP also provides some utility methods for sending mail
  33   * to an SMTP server.
  34   * @package PHPMailer
  35   * @author Chris Ryan
  36   */
  37  
  38  class SMTP
  39  {
  40    /**
  41     *  SMTP server port
  42     *  @var int
  43     */
  44    var $SMTP_PORT = 25;
  45  
  46    /**
  47     *  SMTP reply line ending
  48     *  @var string
  49     */
  50    var $CRLF = "\r\n";
  51  
  52    /**
  53     *  Sets whether debugging is turned on
  54     *  @var bool
  55     */
  56    var $do_debug;       # the level of debug to perform
  57  
  58    /**
  59     *  Sets VERP use on/off (default is off)
  60     *  @var bool
  61     */
  62    var $do_verp = false;
  63  
  64    /**#@+
  65     * @access private
  66     */
  67    var $smtp_conn;      # the socket to the server
  68    var $error;          # error if any on the last call
  69    var $helo_rply;      # the reply the server sent to us for HELO
  70    /**#@-*/
  71  
  72    /**
  73     * Initialize the class so that the data is in a known state.
  74     * @access public
  75     * @return void
  76     */
  77    function SMTP() {
  78      $this->smtp_conn = 0;
  79      $this->error = null;
  80      $this->helo_rply = null;
  81  
  82      $this->do_debug = 0;
  83    }
  84  
  85    /*************************************************************
  86     *                    CONNECTION FUNCTIONS                  *
  87     ***********************************************************/
  88  
  89    /**
  90     * Connect to the server specified on the port specified.
  91     * If the port is not specified use the default SMTP_PORT.
  92     * If tval is specified then a connection will try and be
  93     * established with the server for that number of seconds.
  94     * If tval is not specified the default is 30 seconds to
  95     * try on the connection.
  96     *
  97     * SMTP CODE SUCCESS: 220
  98     * SMTP CODE FAILURE: 421
  99     * @access public
 100     * @return bool
 101     */
 102    function Connect($host,$port=0,$tval=30) {
 103      # set the error val to null so there is no confusion
 104      $this->error = null;
 105  
 106      # make sure we are __not__ connected
 107      if($this->connected()) {
 108        # ok we are connected! what should we do?
 109        # for now we will just give an error saying we
 110        # are already connected
 111        $this->error = array("error" => "Already connected to a server");
 112        return false;
 113      }
 114  
 115      if(empty($port)) {
 116        $port = $this->SMTP_PORT;
 117      }
 118  
 119      #connect to the smtp server
 120      $this->smtp_conn = fsockopen($host,    # the host of the server
 121                                   $port,    # the port to use
 122                                   $errno,   # error number if any
 123                                   $errstr,  # error message if any
 124                                   $tval);   # give up after ? secs
 125      # verify we connected properly
 126      if(empty($this->smtp_conn)) {
 127        $this->error = array("error" => "Failed to connect to server",
 128                             "errno" => $errno,
 129                             "errstr" => $errstr);
 130        if($this->do_debug >= 1) {
 131          echo "SMTP -> ERROR: " . $this->error["error"] .
 132                   ": $errstr ($errno)" . $this->CRLF;
 133        }
 134        return false;
 135      }
 136  
 137      # sometimes the SMTP server takes a little longer to respond
 138      # so we will give it a longer timeout for the first read
 139      // Windows still does not have support for this timeout function
 140      if(substr(PHP_OS, 0, 3) != "WIN")
 141       socket_set_timeout($this->smtp_conn, $tval, 0);
 142  
 143      # get any announcement stuff
 144      $announce = $this->get_lines();
 145  
 146      # set the timeout  of any socket functions at 1/10 of a second
 147      //if(function_exists("socket_set_timeout"))
 148      //   socket_set_timeout($this->smtp_conn, 0, 100000);
 149  
 150      if($this->do_debug >= 2) {
 151        echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
 152      }
 153  
 154      return true;
 155    }
 156  
 157    /**
 158     * Performs SMTP authentication.  Must be run after running the
 159     * Hello() method.  Returns true if successfully authenticated.
 160     * @access public
 161     * @return bool
 162     */
 163    function Authenticate($username, $password) {
 164      // Start authentication
 165      fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
 166  
 167      $rply = $this->get_lines();
 168      $code = substr($rply,0,3);
 169  
 170      if($code != 334) {
 171        $this->error =
 172          array("error" => "AUTH not accepted from server",
 173                "smtp_code" => $code,
 174                "smtp_msg" => substr($rply,4));
 175        if($this->do_debug >= 1) {
 176          echo "SMTP -> ERROR: " . $this->error["error"] .
 177                   ": " . $rply . $this->CRLF;
 178        }
 179        return false;
 180      }
 181  
 182      // Send encoded username
 183      fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
 184  
 185      $rply = $this->get_lines();
 186      $code = substr($rply,0,3);
 187  
 188      if($code != 334) {
 189        $this->error =
 190          array("error" => "Username not accepted from server",
 191                "smtp_code" => $code,
 192                "smtp_msg" => substr($rply,4));
 193        if($this->do_debug >= 1) {
 194          echo "SMTP -> ERROR: " . $this->error["error"] .
 195                   ": " . $rply . $this->CRLF;
 196        }
 197        return false;
 198      }
 199  
 200      // Send encoded password
 201      fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
 202  
 203      $rply = $this->get_lines();
 204      $code = substr($rply,0,3);
 205  
 206      if($code != 235) {
 207        $this->error =
 208          array("error" => "Password not accepted from server",
 209                "smtp_code" => $code,
 210                "smtp_msg" => substr($rply,4));
 211        if($this->do_debug >= 1) {
 212          echo "SMTP -> ERROR: " . $this->error["error"] .
 213                   ": " . $rply . $this->CRLF;
 214        }
 215        return false;
 216      }
 217  
 218      return true;
 219    }
 220  
 221    /**
 222     * Returns true if connected to a server otherwise false
 223     * @access private
 224     * @return bool
 225     */
 226    function Connected() {
 227      if(!empty($this->smtp_conn)) {
 228        $sock_status = socket_get_status($this->smtp_conn);
 229        if($sock_status["eof"]) {
 230          # hmm this is an odd situation... the socket is
 231          # valid but we are not connected anymore
 232          if($this->do_debug >= 1) {
 233              echo "SMTP -> NOTICE:" . $this->CRLF .
 234                   "EOF caught while checking if connected";
 235          }
 236          $this->Close();
 237          return false;
 238        }
 239        return true; # everything looks good
 240      }
 241      return false;
 242    }
 243  
 244    /**
 245     * Closes the socket and cleans up the state of the class.
 246     * It is not considered good to use this function without
 247     * first trying to use QUIT.
 248     * @access public
 249     * @return void
 250     */
 251    function Close() {
 252      $this->error = null; # so there is no confusion
 253      $this->helo_rply = null;
 254      if(!empty($this->smtp_conn)) {
 255        # close the connection and cleanup
 256        fclose($this->smtp_conn);
 257        $this->smtp_conn = 0;
 258      }
 259    }
 260  
 261    /***************************************************************
 262     *                        SMTP COMMANDS                       *
 263     *************************************************************/
 264  
 265    /**
 266     * Issues a data command and sends the msg_data to the server
 267     * finializing the mail transaction. $msg_data is the message
 268     * that is to be send with the headers. Each header needs to be
 269     * on a single line followed by a <CRLF> with the message headers
 270     * and the message body being separated by and additional <CRLF>.
 271     *
 272     * Implements rfc 821: DATA <CRLF>
 273     *
 274     * SMTP CODE INTERMEDIATE: 354
 275     *     [data]
 276     *     <CRLF>.<CRLF>
 277     *     SMTP CODE SUCCESS: 250
 278     *     SMTP CODE FAILURE: 552,554,451,452
 279     * SMTP CODE FAILURE: 451,554
 280     * SMTP CODE ERROR  : 500,501,503,421
 281     * @access public
 282     * @return bool
 283     */
 284    function Data($msg_data) {
 285      $this->error = null; # so no confusion is caused
 286  
 287      if(!$this->connected()) {
 288        $this->error = array(
 289                "error" => "Called Data() without being connected");
 290        return false;
 291      }
 292  
 293      fputs($this->smtp_conn,"DATA" . $this->CRLF);
 294  
 295      $rply = $this->get_lines();
 296      $code = substr($rply,0,3);
 297  
 298      if($this->do_debug >= 2) {
 299        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 300      }
 301  
 302      if($code != 354) {
 303        $this->error =
 304          array("error" => "DATA command not accepted from server",
 305                "smtp_code" => $code,
 306                "smtp_msg" => substr($rply,4));
 307        if($this->do_debug >= 1) {
 308          echo "SMTP -> ERROR: " . $this->error["error"] .
 309                   ": " . $rply . $this->CRLF;
 310        }
 311        return false;
 312      }
 313  
 314      # the server is ready to accept data!
 315      # according to rfc 821 we should not send more than 1000
 316      # including the CRLF
 317      # characters on a single line so we will break the data up
 318      # into lines by \r and/or \n then if needed we will break
 319      # each of those into smaller lines to fit within the limit.
 320      # in addition we will be looking for lines that start with
 321      # a period '.' and append and additional period '.' to that
 322      # line. NOTE: this does not count towards are limit.
 323  
 324      # normalize the line breaks so we know the explode works
 325      $msg_data = str_replace("\r\n","\n",$msg_data);
 326      $msg_data = str_replace("\r","\n",$msg_data);
 327      $lines = explode("\n",$msg_data);
 328  
 329      # we need to find a good way to determine is headers are
 330      # in the msg_data or if it is a straight msg body
 331      # currently I am assuming rfc 822 definitions of msg headers
 332      # and if the first field of the first line (':' sperated)
 333      # does not contain a space then it _should_ be a header
 334      # and we can process all lines before a blank "" line as
 335      # headers.
 336      $field = substr($lines[0],0,strpos($lines[0],":"));
 337      $in_headers = false;
 338      if(!empty($field) && !strstr($field," ")) {
 339        $in_headers = true;
 340      }
 341  
 342      $max_line_length = 998; # used below; set here for ease in change
 343  
 344      while(list(,$line) = @each($lines)) {
 345        $lines_out = null;
 346        if($line == "" && $in_headers) {
 347          $in_headers = false;
 348        }
 349        # ok we need to break this line up into several
 350        # smaller lines
 351        while(strlen($line) > $max_line_length) {
 352          $pos = strrpos(substr($line,0,$max_line_length)," ");
 353  
 354          # Patch to fix DOS attack
 355          if(!$pos) {
 356            $pos = $max_line_length - 1;
 357          }
 358  
 359          $lines_out[] = substr($line,0,$pos);
 360          $line = substr($line,$pos + 1);
 361          # if we are processing headers we need to
 362          # add a LWSP-char to the front of the new line
 363          # rfc 822 on long msg headers
 364          if($in_headers) {
 365            $line = "\t" . $line;
 366          }
 367        }
 368        $lines_out[] = $line;
 369  
 370        # now send the lines to the server
 371        while(list(,$line_out) = @each($lines_out)) {
 372          if(strlen($line_out) > 0)
 373          {
 374            if(substr($line_out, 0, 1) == ".") {
 375              $line_out = "." . $line_out;
 376            }
 377          }
 378          fputs($this->smtp_conn,$line_out . $this->CRLF);
 379        }
 380      }
 381  
 382      # ok all the message data has been sent so lets get this
 383      # over with aleady
 384      fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
 385  
 386      $rply = $this->get_lines();
 387      $code = substr($rply,0,3);
 388  
 389      if($this->do_debug >= 2) {
 390        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 391      }
 392  
 393      if($code != 250) {
 394        $this->error =
 395          array("error" => "DATA not accepted from server",
 396                "smtp_code" => $code,
 397                "smtp_msg" => substr($rply,4));
 398        if($this->do_debug >= 1) {
 399          echo "SMTP -> ERROR: " . $this->error["error"] .
 400                   ": " . $rply . $this->CRLF;
 401        }
 402        return false;
 403      }
 404      return true;
 405    }
 406  
 407    /**
 408     * Expand takes the name and asks the server to list all the
 409     * people who are members of the _list_. Expand will return
 410     * back and array of the result or false if an error occurs.
 411     * Each value in the array returned has the format of:
 412     *     [ <full-name> <sp> ] <path>
 413     * The definition of <path> is defined in rfc 821
 414     *
 415     * Implements rfc 821: EXPN <SP> <string> <CRLF>
 416     *
 417     * SMTP CODE SUCCESS: 250
 418     * SMTP CODE FAILURE: 550
 419     * SMTP CODE ERROR  : 500,501,502,504,421
 420     * @access public
 421     * @return string array
 422     */
 423    function Expand($name) {
 424      $this->error = null; # so no confusion is caused
 425  
 426      if(!$this->connected()) {
 427        $this->error = array(
 428              "error" => "Called Expand() without being connected");
 429        return false;
 430      }
 431  
 432      fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF);
 433  
 434      $rply = $this->get_lines();
 435      $code = substr($rply,0,3);
 436  
 437      if($this->do_debug >= 2) {
 438        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 439      }
 440  
 441      if($code != 250) {
 442        $this->error =
 443          array("error" => "EXPN not accepted from server",
 444                "smtp_code" => $code,
 445                "smtp_msg" => substr($rply,4));
 446        if($this->do_debug >= 1) {
 447          echo "SMTP -> ERROR: " . $this->error["error"] .
 448                   ": " . $rply . $this->CRLF;
 449        }
 450        return false;
 451      }
 452  
 453      # parse the reply and place in our array to return to user
 454      $entries = explode($this->CRLF,$rply);
 455      while(list(,$l) = @each($entries)) {
 456        $list[] = substr($l,4);
 457      }
 458  
 459      return $list;
 460    }
 461  
 462    /**
 463     * Sends the HELO command to the smtp server.
 464     * This makes sure that we and the server are in
 465     * the same known state.
 466     *
 467     * Implements from rfc 821: HELO <SP> <domain> <CRLF>
 468     *
 469     * SMTP CODE SUCCESS: 250
 470     * SMTP CODE ERROR  : 500, 501, 504, 421
 471     * @access public
 472     * @return bool
 473     */
 474    function Hello($host="") {
 475      $this->error = null; # so no confusion is caused
 476  
 477      if(!$this->connected()) {
 478        $this->error = array(
 479              "error" => "Called Hello() without being connected");
 480        return false;
 481      }
 482  
 483      # if a hostname for the HELO was not specified determine
 484      # a suitable one to send
 485      if(empty($host)) {
 486        # we need to determine some sort of appopiate default
 487        # to send to the server
 488        $host = "localhost";
 489      }
 490  
 491      // Send extended hello first (RFC 2821)
 492      if(!$this->SendHello("EHLO", $host))
 493      {
 494        if(!$this->SendHello("HELO", $host))
 495            return false;
 496      }
 497  
 498      return true;
 499    }
 500  
 501    /**
 502     * Sends a HELO/EHLO command.
 503     * @access private
 504     * @return bool
 505     */
 506    function SendHello($hello, $host) {
 507      fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
 508  
 509      $rply = $this->get_lines();
 510      $code = substr($rply,0,3);
 511  
 512      if($this->do_debug >= 2) {
 513        echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
 514      }
 515  
 516      if($code != 250) {
 517        $this->error =
 518          array("error" => $hello . " not accepted from server",
 519                "smtp_code" => $code,
 520                "smtp_msg" => substr($rply,4));
 521        if($this->do_debug >= 1) {
 522          echo "SMTP -> ERROR: " . $this->error["error"] .
 523                   ": " . $rply . $this->CRLF;
 524        }
 525        return false;
 526      }
 527  
 528      $this->helo_rply = $rply;
 529  
 530      return true;
 531    }
 532  
 533    /**
 534     * Gets help information on the keyword specified. If the keyword
 535     * is not specified then returns generic help, ussually contianing
 536     * A list of keywords that help is available on. This function
 537     * returns the results back to the user. It is up to the user to
 538     * handle the returned data. If an error occurs then false is
 539     * returned with $this->error set appropiately.
 540     *
 541     * Implements rfc 821: HELP [ <SP> <string> ] <CRLF>
 542     *
 543     * SMTP CODE SUCCESS: 211,214
 544     * SMTP CODE ERROR  : 500,501,502,504,421
 545     * @access public
 546     * @return string
 547     */
 548    function Help($keyword="") {
 549      $this->error = null; # to avoid confusion
 550  
 551      if(!$this->connected()) {
 552        $this->error = array(
 553                "error" => "Called Help() without being connected");
 554        return false;
 555      }
 556  
 557      $extra = "";
 558      if(!empty($keyword)) {
 559        $extra = " " . $keyword;
 560      }
 561  
 562      fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF);
 563  
 564      $rply = $this->get_lines();
 565      $code = substr($rply,0,3);
 566  
 567      if($this->do_debug >= 2) {
 568        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 569      }
 570  
 571      if($code != 211 && $code != 214) {
 572        $this->error =
 573          array("error" => "HELP not accepted from server",
 574                "smtp_code" => $code,
 575                "smtp_msg" => substr($rply,4));
 576        if($this->do_debug >= 1) {
 577          echo "SMTP -> ERROR: " . $this->error["error"] .
 578                   ": " . $rply . $this->CRLF;
 579        }
 580        return false;
 581      }
 582  
 583      return $rply;
 584    }
 585  
 586    /**
 587     * Starts a mail transaction from the email address specified in
 588     * $from. Returns true if successful or false otherwise. If True
 589     * the mail transaction is started and then one or more Recipient
 590     * commands may be called followed by a Data command.
 591     *
 592     * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
 593     *
 594     * SMTP CODE SUCCESS: 250
 595     * SMTP CODE SUCCESS: 552,451,452
 596     * SMTP CODE SUCCESS: 500,501,421
 597     * @access public
 598     * @return bool
 599     */
 600    function Mail($from) {
 601      $this->error = null; # so no confusion is caused
 602  
 603      if(!$this->connected()) {
 604        $this->error = array(
 605                "error" => "Called Mail() without being connected");
 606        return false;
 607      }
 608  
 609      $useVerp = ($this->do_verp ? "XVERP" : "");
 610      fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
 611  
 612      $rply = $this->get_lines();
 613      $code = substr($rply,0,3);
 614  
 615      if($this->do_debug >= 2) {
 616        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 617      }
 618  
 619      if($code != 250) {
 620        $this->error =
 621          array("error" => "MAIL not accepted from server",
 622                "smtp_code" => $code,
 623                "smtp_msg" => substr($rply,4));
 624        if($this->do_debug >= 1) {
 625          echo "SMTP -> ERROR: " . $this->error["error"] .
 626                   ": " . $rply . $this->CRLF;
 627        }
 628        return false;
 629      }
 630      return true;
 631    }
 632  
 633    /**
 634     * Sends the command NOOP to the SMTP server.
 635     *
 636     * Implements from rfc 821: NOOP <CRLF>
 637     *
 638     * SMTP CODE SUCCESS: 250
 639     * SMTP CODE ERROR  : 500, 421
 640     * @access public
 641     * @return bool
 642     */
 643    function Noop() {
 644      $this->error = null; # so no confusion is caused
 645  
 646      if(!$this->connected()) {
 647        $this->error = array(
 648                "error" => "Called Noop() without being connected");
 649        return false;
 650      }
 651  
 652      fputs($this->smtp_conn,"NOOP" . $this->CRLF);
 653  
 654      $rply = $this->get_lines();
 655      $code = substr($rply,0,3);
 656  
 657      if($this->do_debug >= 2) {
 658        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 659      }
 660  
 661      if($code != 250) {
 662        $this->error =
 663          array("error" => "NOOP not accepted from server",
 664                "smtp_code" => $code,
 665                "smtp_msg" => substr($rply,4));
 666        if($this->do_debug >= 1) {
 667          echo "SMTP -> ERROR: " . $this->error["error"] .
 668                   ": " . $rply . $this->CRLF;
 669        }
 670        return false;
 671      }
 672      return true;
 673    }
 674  
 675    /**
 676     * Sends the quit command to the server and then closes the socket
 677     * if there is no error or the $close_on_error argument is true.
 678     *
 679     * Implements from rfc 821: QUIT <CRLF>
 680     *
 681     * SMTP CODE SUCCESS: 221
 682     * SMTP CODE ERROR  : 500
 683     * @access public
 684     * @return bool
 685     */
 686    function Quit($close_on_error=true) {
 687      $this->error = null; # so there is no confusion
 688  
 689      if(!$this->connected()) {
 690        $this->error = array(
 691                "error" => "Called Quit() without being connected");
 692        return false;
 693      }
 694  
 695      # send the quit command to the server
 696      fputs($this->smtp_conn,"quit" . $this->CRLF);
 697  
 698      # get any good-bye messages
 699      $byemsg = $this->get_lines();
 700  
 701      if($this->do_debug >= 2) {
 702        echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
 703      }
 704  
 705      $rval = true;
 706      $e = null;
 707  
 708      $code = substr($byemsg,0,3);
 709      if($code != 221) {
 710        # use e as a tmp var cause Close will overwrite $this->error
 711        $e = array("error" => "SMTP server rejected quit command",
 712                   "smtp_code" => $code,
 713                   "smtp_rply" => substr($byemsg,4));
 714        $rval = false;
 715        if($this->do_debug >= 1) {
 716          echo "SMTP -> ERROR: " . $e["error"] . ": " .
 717                   $byemsg . $this->CRLF;
 718        }
 719      }
 720  
 721      if(empty($e) || $close_on_error) {
 722        $this->Close();
 723      }
 724  
 725      return $rval;
 726    }
 727  
 728    /**
 729     * Sends the command RCPT to the SMTP server with the TO: argument of $to.
 730     * Returns true if the recipient was accepted false if it was rejected.
 731     *
 732     * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
 733     *
 734     * SMTP CODE SUCCESS: 250,251
 735     * SMTP CODE FAILURE: 550,551,552,553,450,451,452
 736     * SMTP CODE ERROR  : 500,501,503,421
 737     * @access public
 738     * @return bool
 739     */
 740    function Recipient($to) {
 741      $this->error = null; # so no confusion is caused
 742  
 743      if(!$this->connected()) {
 744        $this->error = array(
 745                "error" => "Called Recipient() without being connected");
 746        return false;
 747      }
 748  
 749      fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
 750  
 751      $rply = $this->get_lines();
 752      $code = substr($rply,0,3);
 753  
 754      if($this->do_debug >= 2) {
 755        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 756      }
 757  
 758      if($code != 250 && $code != 251) {
 759        $this->error =
 760          array("error" => "RCPT not accepted from server",
 761                "smtp_code" => $code,
 762                "smtp_msg" => substr($rply,4));
 763        if($this->do_debug >= 1) {
 764          echo "SMTP -> ERROR: " . $this->error["error"] .
 765                   ": " . $rply . $this->CRLF;
 766        }
 767        return false;
 768      }
 769      return true;
 770    }
 771  
 772    /**
 773     * Sends the RSET command to abort and transaction that is
 774     * currently in progress. Returns true if successful false
 775     * otherwise.
 776     *
 777     * Implements rfc 821: RSET <CRLF>
 778     *
 779     * SMTP CODE SUCCESS: 250
 780     * SMTP CODE ERROR  : 500,501,504,421
 781     * @access public
 782     * @return bool
 783     */
 784    function Reset() {
 785      $this->error = null; # so no confusion is caused
 786  
 787      if(!$this->connected()) {
 788        $this->error = array(
 789                "error" => "Called Reset() without being connected");
 790        return false;
 791      }
 792  
 793      fputs($this->smtp_conn,"RSET" . $this->CRLF);
 794  
 795      $rply = $this->get_lines();
 796      $code = substr($rply,0,3);
 797  
 798      if($this->do_debug >= 2) {
 799        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 800      }
 801  
 802      if($code != 250) {
 803        $this->error =
 804          array("error" => "RSET failed",
 805                "smtp_code" => $code,
 806                "smtp_msg" => substr($rply,4));
 807        if($this->do_debug >= 1) {
 808          echo "SMTP -> ERROR: " . $this->error["error"] .
 809                   ": " . $rply . $this->CRLF;
 810        }
 811        return false;
 812      }
 813  
 814      return true;
 815    }
 816  
 817    /**
 818     * Starts a mail transaction from the email address specified in
 819     * $from. Returns true if successful or false otherwise. If True
 820     * the mail transaction is started and then one or more Recipient
 821     * commands may be called followed by a Data command. This command
 822     * will send the message to the users terminal if they are logged
 823     * in.
 824     *
 825     * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF>
 826     *
 827     * SMTP CODE SUCCESS: 250
 828     * SMTP CODE SUCCESS: 552,451,452
 829     * SMTP CODE SUCCESS: 500,501,502,421
 830     * @access public
 831     * @return bool
 832     */
 833    function Send($from) {
 834      $this->error = null; # so no confusion is caused
 835  
 836      if(!$this->connected()) {
 837        $this->error = array(
 838                "error" => "Called Send() without being connected");
 839        return false;
 840      }
 841  
 842      fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF);
 843  
 844      $rply = $this->get_lines();
 845      $code = substr($rply,0,3);
 846  
 847      if($this->do_debug >= 2) {
 848        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 849      }
 850  
 851      if($code != 250) {
 852        $this->error =
 853          array("error" => "SEND not accepted from server",
 854                "smtp_code" => $code,
 855                "smtp_msg" => substr($rply,4));
 856        if($this->do_debug >= 1) {
 857          echo "SMTP -> ERROR: " . $this->error["error"] .
 858                   ": " . $rply . $this->CRLF;
 859        }
 860        return false;
 861      }
 862      return true;
 863    }
 864  
 865    /**
 866     * Starts a mail transaction from the email address specified in
 867     * $from. Returns true if successful or false otherwise. If True
 868     * the mail transaction is started and then one or more Recipient
 869     * commands may be called followed by a Data command. This command
 870     * will send the message to the users terminal if they are logged
 871     * in and send them an email.
 872     *
 873     * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
 874     *
 875     * SMTP CODE SUCCESS: 250
 876     * SMTP CODE SUCCESS: 552,451,452
 877     * SMTP CODE SUCCESS: 500,501,502,421
 878     * @access public
 879     * @return bool
 880     */
 881    function SendAndMail($from) {
 882      $this->error = null; # so no confusion is caused
 883  
 884      if(!$this->connected()) {
 885        $this->error = array(
 886            "error" => "Called SendAndMail() without being connected");
 887        return false;
 888      }
 889  
 890      fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
 891  
 892      $rply = $this->get_lines();
 893      $code = substr($rply,0,3);
 894  
 895      if($this->do_debug >= 2) {
 896        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 897      }
 898  
 899      if($code != 250) {
 900        $this->error =
 901          array("error" => "SAML not accepted from server",
 902                "smtp_code" => $code,
 903                "smtp_msg" => substr($rply,4));
 904        if($this->do_debug >= 1) {
 905          echo "SMTP -> ERROR: " . $this->error["error"] .
 906                   ": " . $rply . $this->CRLF;
 907        }
 908        return false;
 909      }
 910      return true;
 911    }
 912  
 913    /**
 914     * Starts a mail transaction from the email address specified in
 915     * $from. Returns true if successful or false otherwise. If True
 916     * the mail transaction is started and then one or more Recipient
 917     * commands may be called followed by a Data command. This command
 918     * will send the message to the users terminal if they are logged
 919     * in or mail it to them if they are not.
 920     *
 921     * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF>
 922     *
 923     * SMTP CODE SUCCESS: 250
 924     * SMTP CODE SUCCESS: 552,451,452
 925     * SMTP CODE SUCCESS: 500,501,502,421
 926     * @access public
 927     * @return bool
 928     */
 929    function SendOrMail($from) {
 930      $this->error = null; # so no confusion is caused
 931  
 932      if(!$this->connected()) {
 933        $this->error = array(
 934            "error" => "Called SendOrMail() without being connected");
 935        return false;
 936      }
 937  
 938      fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF);
 939  
 940      $rply = $this->get_lines();
 941      $code = substr($rply,0,3);
 942  
 943      if($this->do_debug >= 2) {
 944        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
 945      }
 946  
 947      if($code != 250) {
 948        $this->error =
 949          array("error" => "SOML not accepted from server",
 950                "smtp_code" => $code,
 951                "smtp_msg" => substr($rply,4));
 952        if($this->do_debug >= 1) {
 953          echo "SMTP -> ERROR: " . $this->error["error"] .
 954                   ": " . $rply . $this->CRLF;
 955        }
 956        return false;
 957      }
 958      return true;
 959    }
 960  
 961    /**
 962     * This is an optional command for SMTP that this class does not
 963     * support. This method is here to make the RFC821 Definition
 964     * complete for this class and __may__ be implimented in the future
 965     *
 966     * Implements from rfc 821: TURN <CRLF>
 967     *
 968     * SMTP CODE SUCCESS: 250
 969     * SMTP CODE FAILURE: 502
 970     * SMTP CODE ERROR  : 500, 503
 971     * @access public
 972     * @return bool
 973     */
 974    function Turn() {
 975      $this->error = array("error" => "This method, TURN, of the SMTP ".
 976                                      "is not implemented");
 977      if($this->do_debug >= 1) {
 978        echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF;
 979      }
 980      return false;
 981    }
 982  
 983    /**
 984     * Verifies that the name is recognized by the server.
 985     * Returns false if the name could not be verified otherwise
 986     * the response from the server is returned.
 987     *
 988     * Implements rfc 821: VRFY <SP> <string> <CRLF>
 989     *
 990     * SMTP CODE SUCCESS: 250,251
 991     * SMTP CODE FAILURE: 550,551,553
 992     * SMTP CODE ERROR  : 500,501,502,421
 993     * @access public
 994     * @return int
 995     */
 996    function Verify($name) {
 997      $this->error = null; # so no confusion is caused
 998  
 999      if(!$this->connected()) {
1000        $this->error = array(
1001                "error" => "Called Verify() without being connected");
1002        return false;
1003      }
1004  
1005      fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF);
1006  
1007      $rply = $this->get_lines();
1008      $code = substr($rply,0,3);
1009  
1010      if($this->do_debug >= 2) {
1011        echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
1012      }
1013  
1014      if($code != 250 && $code != 251) {
1015        $this->error =
1016          array("error" => "VRFY failed on name '$name'",
1017                "smtp_code" => $code,
1018                "smtp_msg" => substr($rply,4));
1019        if($this->do_debug >= 1) {
1020          echo "SMTP -> ERROR: " . $this->error["error"] .
1021                   ": " . $rply . $this->CRLF;
1022        }
1023        return false;
1024      }
1025      return $rply;
1026    }
1027  
1028    /*******************************************************************
1029     *                       INTERNAL FUNCTIONS                       *
1030     ******************************************************************/
1031  
1032    /**
1033     * Read in as many lines as possible
1034     * either before eof or socket timeout occurs on the operation.
1035     * With SMTP we can tell if we have more lines to read if the
1036     * 4th character is '-' symbol. If it is a space then we don't
1037     * need to read anything else.
1038     * @access private
1039     * @return string
1040     */
1041    function get_lines() {
1042      $data = "";
1043      while($str = @fgets($this->smtp_conn,515)) {
1044        if($this->do_debug >= 4) {
1045          echo "SMTP -> get_lines(): \$data was \"$data\"" .
1046                   $this->CRLF;
1047          echo "SMTP -> get_lines(): \$str is \"$str\"" .
1048                   $this->CRLF;
1049        }
1050        $data .= $str;
1051        if($this->do_debug >= 4) {
1052          echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
1053        }
1054        # if the 4th character is a space then we are done reading
1055        # so just break the loop
1056        if(substr($str,3,1) == " ") { break; }
1057      }
1058      return $data;
1059    }
1060  
1061  }


Generated: Wed Feb 8 03:57:40 2012 Hosted by follow the white rabbit.