class.pop3.php 10.7 KB
Newer Older
1
2
<?php
/**
Aaron Wells's avatar
Aaron Wells committed
3
 * PHPMailer POP-Before-SMTP Authentication Class.
4
 * PHP Version 5
5
 * @package PHPMailer
Aaron Wells's avatar
Aaron Wells committed
6
 * @link https://github.com/PHPMailer/PHPMailer/
7
 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
Aaron Wells's avatar
Aaron Wells committed
8
9
10
 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
 * @author Brent R. Matzelle (original founder)
11
 * @copyright 2012 - 2014 Marcus Bointon
Aaron Wells's avatar
Aaron Wells committed
12
 * @copyright 2010 - 2012 Jim Jagielski
13
 * @copyright 2004 - 2009 Andy Prevost
Aaron Wells's avatar
Aaron Wells committed
14
15
16
17
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @note This program is distributed in the hope that it will be useful - WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
18
19
20
 */

/**
Aaron Wells's avatar
Aaron Wells committed
21
22
23
 * PHPMailer POP-Before-SMTP Authentication Class.
 * Specifically for PHPMailer to use for RFC1939 POP-before-SMTP authentication.
 * Does not support APOP.
24
 * @package PHPMailer
Aaron Wells's avatar
Aaron Wells committed
25
 * @author Richard Davey (original author) <rich@corephp.co.uk>
26
 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
Aaron Wells's avatar
Aaron Wells committed
27
28
 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
29
 */
Aaron Wells's avatar
Aaron Wells committed
30
31
32
33
class POP3
{
    /**
     * The POP3 PHPMailer Version number.
34
     * @var string
Aaron Wells's avatar
Aaron Wells committed
35
36
     * @access public
     */
37
    public $Version = '5.2.14';
Aaron Wells's avatar
Aaron Wells committed
38
39
40

    /**
     * Default POP3 port number.
41
     * @var integer
Aaron Wells's avatar
Aaron Wells committed
42
43
44
45
46
47
     * @access public
     */
    public $POP3_PORT = 110;

    /**
     * Default timeout in seconds.
48
     * @var integer
Aaron Wells's avatar
Aaron Wells committed
49
50
51
52
53
54
     * @access public
     */
    public $POP3_TIMEOUT = 30;

    /**
     * POP3 Carriage Return + Line Feed.
55
     * @var string
Aaron Wells's avatar
Aaron Wells committed
56
57
58
59
60
61
62
63
     * @access public
     * @deprecated Use the constant instead
     */
    public $CRLF = "\r\n";

    /**
     * Debug display level.
     * Options: 0 = no, 1+ = yes
64
     * @var integer
Aaron Wells's avatar
Aaron Wells committed
65
66
67
68
69
70
     * @access public
     */
    public $do_debug = 0;

    /**
     * POP3 mail server hostname.
71
     * @var string
Aaron Wells's avatar
Aaron Wells committed
72
73
74
75
76
77
     * @access public
     */
    public $host;

    /**
     * POP3 port number.
78
     * @var integer
Aaron Wells's avatar
Aaron Wells committed
79
80
81
82
83
84
     * @access public
     */
    public $port;

    /**
     * POP3 Timeout Value in seconds.
85
     * @var integer
Aaron Wells's avatar
Aaron Wells committed
86
87
88
89
90
91
     * @access public
     */
    public $tval;

    /**
     * POP3 username
92
     * @var string
Aaron Wells's avatar
Aaron Wells committed
93
94
95
96
97
98
     * @access public
     */
    public $username;

    /**
     * POP3 password.
99
     * @var string
Aaron Wells's avatar
Aaron Wells committed
100
101
102
103
104
105
     * @access public
     */
    public $password;

    /**
     * Resource handle for the POP3 connection socket.
106
107
     * @var resource
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
108
     */
109
    protected $pop_conn;
Aaron Wells's avatar
Aaron Wells committed
110
111
112

    /**
     * Are we connected?
113
114
     * @var boolean
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
115
     */
116
    protected $connected = false;
Aaron Wells's avatar
Aaron Wells committed
117
118
119

    /**
     * Error container.
120
121
     * @var array
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
122
     */
123
    protected $errors = array();
Aaron Wells's avatar
Aaron Wells committed
124
125
126
127
128
129
130
131
132

    /**
     * Line break constant
     */
    const CRLF = "\r\n";

    /**
     * Simple static wrapper for all-in-one POP before SMTP
     * @param $host
133
134
     * @param integer|boolean $port The port number to connect to
     * @param integer|boolean $timeout The timeout value
Aaron Wells's avatar
Aaron Wells committed
135
136
     * @param string $username
     * @param string $password
137
138
     * @param integer $debug_level
     * @return boolean
Aaron Wells's avatar
Aaron Wells committed
139
140
141
142
     */
    public static function popBeforeSmtp(
        $host,
        $port = false,
143
        $timeout = false,
Aaron Wells's avatar
Aaron Wells committed
144
145
146
147
148
        $username = '',
        $password = '',
        $debug_level = 0
    ) {
        $pop = new POP3;
149
        return $pop->authorise($host, $port, $timeout, $username, $password, $debug_level);
150
151
    }

Aaron Wells's avatar
Aaron Wells committed
152
153
154
155
156
    /**
     * Authenticate with a POP3 server.
     * A connect, login, disconnect sequence
     * appropriate for POP-before SMTP authorisation.
     * @access public
157
158
159
     * @param string $host The hostname to connect to
     * @param integer|boolean $port The port number to connect to
     * @param integer|boolean $timeout The timeout value
Aaron Wells's avatar
Aaron Wells committed
160
161
     * @param string $username
     * @param string $password
162
163
     * @param integer $debug_level
     * @return boolean
Aaron Wells's avatar
Aaron Wells committed
164
     */
165
    public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
Aaron Wells's avatar
Aaron Wells committed
166
167
168
    {
        $this->host = $host;
        // If no port value provided, use default
169
        if (false === $port) {
Aaron Wells's avatar
Aaron Wells committed
170
171
            $this->port = $this->POP3_PORT;
        } else {
172
            $this->port = (integer)$port;
Aaron Wells's avatar
Aaron Wells committed
173
174
        }
        // If no timeout value provided, use default
175
        if (false === $timeout) {
Aaron Wells's avatar
Aaron Wells committed
176
177
            $this->tval = $this->POP3_TIMEOUT;
        } else {
178
            $this->tval = (integer)$timeout;
Aaron Wells's avatar
Aaron Wells committed
179
180
181
182
        }
        $this->do_debug = $debug_level;
        $this->username = $username;
        $this->password = $password;
183
184
        //  Reset the error log
        $this->errors = array();
Aaron Wells's avatar
Aaron Wells committed
185
186
187
188
189
190
191
192
193
194
195
196
        //  connect
        $result = $this->connect($this->host, $this->port, $this->tval);
        if ($result) {
            $login_result = $this->login($this->username, $this->password);
            if ($login_result) {
                $this->disconnect();
                return true;
            }
        }
        // We need to disconnect regardless of whether the login succeeded
        $this->disconnect();
        return false;
197
198
    }

Aaron Wells's avatar
Aaron Wells committed
199
200
201
202
    /**
     * Connect to a POP3 server.
     * @access public
     * @param string $host
203
     * @param integer|boolean $port
Aaron Wells's avatar
Aaron Wells committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
     * @param integer $tval
     * @return boolean
     */
    public function connect($host, $port = false, $tval = 30)
    {
        //  Are we already connected?
        if ($this->connected) {
            return true;
        }

        //On Windows this will raise a PHP Warning error if the hostname doesn't exist.
        //Rather than suppress it with @fsockopen, capture it cleanly instead
        set_error_handler(array($this, 'catchWarning'));

218
        if (false === $port) {
219
220
221
            $port = $this->POP3_PORT;
        }

Aaron Wells's avatar
Aaron Wells committed
222
223
224
225
226
227
228
229
230
231
        //  connect to the POP3 server
        $this->pop_conn = fsockopen(
            $host, //  POP3 Host
            $port, //  Port #
            $errno, //  Error Number
            $errstr, //  Error Message
            $tval
        ); //  Timeout (seconds)
        //  Restore the error handler
        restore_error_handler();
232

Aaron Wells's avatar
Aaron Wells committed
233
        //  Did we connect?
234
        if (false === $this->pop_conn) {
Aaron Wells's avatar
Aaron Wells committed
235
            //  It would appear not...
236
            $this->setError(array(
Aaron Wells's avatar
Aaron Wells committed
237
238
239
                'error' => "Failed to connect to server $host on port $port",
                'errno' => $errno,
                'errstr' => $errstr
240
            ));
Aaron Wells's avatar
Aaron Wells committed
241
242
243
244
            return false;
        }

        //  Increase the stream time-out
245
        stream_set_timeout($this->pop_conn, $tval, 0);
Aaron Wells's avatar
Aaron Wells committed
246
247
248
249
250
251
252
253
254
255

        //  Get the POP3 server response
        $pop3_response = $this->getResponse();
        //  Check for the +OK
        if ($this->checkResponse($pop3_response)) {
            //  The connection is established and the POP3 server is talking
            $this->connected = true;
            return true;
        }
        return false;
256
257
    }

Aaron Wells's avatar
Aaron Wells committed
258
259
260
261
262
263
264
265
266
267
    /**
     * Log in to the POP3 server.
     * Does not support APOP (RFC 2828, 4949).
     * @access public
     * @param string $username
     * @param string $password
     * @return boolean
     */
    public function login($username = '', $password = '')
    {
268
269
        if (!$this->connected) {
            $this->setError('Not connected to POP3 server');
Aaron Wells's avatar
Aaron Wells committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
        }
        if (empty($username)) {
            $username = $this->username;
        }
        if (empty($password)) {
            $password = $this->password;
        }

        // Send the Username
        $this->sendString("USER $username" . self::CRLF);
        $pop3_response = $this->getResponse();
        if ($this->checkResponse($pop3_response)) {
            // Send the Password
            $this->sendString("PASS $password" . self::CRLF);
            $pop3_response = $this->getResponse();
            if ($this->checkResponse($pop3_response)) {
                return true;
            }
        }
        return false;
290
291
    }

Aaron Wells's avatar
Aaron Wells committed
292
293
294
295
296
297
298
299
300
    /**
     * Disconnect from the POP3 server.
     * @access public
     */
    public function disconnect()
    {
        $this->sendString('QUIT');
        //The QUIT command may cause the daemon to exit, which will kill our connection
        //So ignore errors here
301
302
303
304
305
        try {
            @fclose($this->pop_conn);
        } catch (Exception $e) {
            //Do nothing
        };
306
307
    }

Aaron Wells's avatar
Aaron Wells committed
308
309
310
311
312
    /**
     * Get a response from the POP3 server.
     * $size is the maximum number of bytes to retrieve
     * @param integer $size
     * @return string
313
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
314
     */
315
    protected function getResponse($size = 128)
Aaron Wells's avatar
Aaron Wells committed
316
    {
317
        $response = fgets($this->pop_conn, $size);
Aaron Wells's avatar
Aaron Wells committed
318
        if ($this->do_debug >= 1) {
319
            echo "Server -> Client: $response";
Aaron Wells's avatar
Aaron Wells committed
320
        }
321
        return $response;
322
323
    }

Aaron Wells's avatar
Aaron Wells committed
324
325
326
327
    /**
     * Send raw data to the POP3 server.
     * @param string $string
     * @return integer
328
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
329
     */
330
    protected function sendString($string)
Aaron Wells's avatar
Aaron Wells committed
331
332
333
334
335
336
337
338
    {
        if ($this->pop_conn) {
            if ($this->do_debug >= 2) { //Show client messages when debug >= 2
                echo "Client -> Server: $string";
            }
            return fwrite($this->pop_conn, $string, strlen($string));
        }
        return 0;
339
340
    }

Aaron Wells's avatar
Aaron Wells committed
341
342
343
344
345
    /**
     * Checks the POP3 server response.
     * Looks for for +OK or -ERR.
     * @param string $string
     * @return boolean
346
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
347
     */
348
    protected function checkResponse($string)
Aaron Wells's avatar
Aaron Wells committed
349
350
    {
        if (substr($string, 0, 3) !== '+OK') {
351
            $this->setError(array(
Aaron Wells's avatar
Aaron Wells committed
352
353
354
                'error' => "Server reported an error: $string",
                'errno' => 0,
                'errstr' => ''
355
            ));
Aaron Wells's avatar
Aaron Wells committed
356
357
358
359
            return false;
        } else {
            return true;
        }
360
361
    }

Aaron Wells's avatar
Aaron Wells committed
362
    /**
363
364
365
     * Add an error to the internal error store.
     * Also display debug output if it's enabled.
     * @param $error
366
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
367
     */
368
    protected function setError($error)
Aaron Wells's avatar
Aaron Wells committed
369
    {
370
371
372
373
374
375
376
        $this->errors[] = $error;
        if ($this->do_debug >= 1) {
            echo '<pre>';
            foreach ($this->errors as $error) {
                print_r($error);
            }
            echo '</pre>';
Aaron Wells's avatar
Aaron Wells committed
377
        }
378
379
    }

380
381
382
383
384
385
386
387
388
    /**
     * Get an array of error messages, if any.
     * @return array
     */
    public function getErrors()
    {
        return $this->errors;
    }

Aaron Wells's avatar
Aaron Wells committed
389
390
391
392
393
394
    /**
     * POP3 connection error handler.
     * @param integer $errno
     * @param string $errstr
     * @param string $errfile
     * @param integer $errline
395
     * @access protected
Aaron Wells's avatar
Aaron Wells committed
396
     */
397
    protected function catchWarning($errno, $errstr, $errfile, $errline)
Aaron Wells's avatar
Aaron Wells committed
398
    {
399
        $this->setError(array(
Aaron Wells's avatar
Aaron Wells committed
400
401
402
403
404
            'error' => "Connecting to the POP3 server raised a PHP warning: ",
            'errno' => $errno,
            'errstr' => $errstr,
            'errfile' => $errfile,
            'errline' => $errline
405
        ));
406
407
    }
}