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