Path: blob/master/externals/phpmailer/class.pop3.php
12241 views
<?php1/*~ class.pop3.php2.---------------------------------------------------------------------------.3| Software: PHPMailer - PHP email class |4| Version: 5.1 |5| Contact: via sourceforge.net support pages (also www.codeworxtech.com) |6| Info: http://phpmailer.sourceforge.net |7| Support: http://sourceforge.net/projects/phpmailer/ |8| ------------------------------------------------------------------------- |9| Admin: Andy Prevost (project admininistrator) |10| Authors: Andy Prevost (codeworxtech) [email protected] |11| : Marcus Bointon (coolbru) [email protected] |12| Founder: Brent R. Matzelle (original founder) |13| Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |14| Copyright (c) 2001-2003, Brent R. Matzelle |15| ------------------------------------------------------------------------- |16| License: Distributed under the Lesser General Public License (LGPL) |17| http://www.gnu.org/copyleft/lesser.html |18| This program is distributed in the hope that it will be useful - WITHOUT |19| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |20| FITNESS FOR A PARTICULAR PURPOSE. |21| ------------------------------------------------------------------------- |22| We offer a number of paid services (www.codeworxtech.com): |23| - Web Hosting on highly optimized fast and secure servers |24| - Technology Consulting |25| - Oursourcing (highly qualified programmers and graphic designers) |26'---------------------------------------------------------------------------'27*/2829/**30* PHPMailer - PHP POP Before SMTP Authentication Class31* NOTE: Designed for use with PHP version 5 and up32* @package PHPMailer33* @author Andy Prevost34* @author Marcus Bointon35* @copyright 2004 - 2009 Andy Prevost36* @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)37* @version $Id: class.pop3.php 444 2009-05-05 11:22:26Z coolbru $38*/3940/**41* POP Before SMTP Authentication Class42* Version 5.0.043*44* Author: Richard Davey ([email protected])45* Modifications: Andy Prevost46* License: LGPL, see PHPMailer License47*48* Specifically for PHPMailer to allow POP before SMTP authentication.49* Does not yet work with APOP - if you have an APOP account, contact Richard Davey50* and we can test changes to this script.51*52* This class is based on the structure of the SMTP class originally authored by Chris Ryan53*54* This class is rfc 1939 compliant and implements all the commands55* required for POP3 connection, authentication and disconnection.56*57* @package PHPMailer58* @author Richard Davey59*/6061class POP3 {62/**63* Default POP3 port64* @var int65*/66public $POP3_PORT = 110;6768/**69* Default Timeout70* @var int71*/72public $POP3_TIMEOUT = 30;7374/**75* POP3 Carriage Return + Line Feed76* @var string77*/78public $CRLF = "\r\n";7980/**81* Displaying Debug warnings? (0 = now, 1+ = yes)82* @var int83*/84public $do_debug = 2;8586/**87* POP3 Mail Server88* @var string89*/90public $host;9192/**93* POP3 Port94* @var int95*/96public $port;9798/**99* POP3 Timeout Value100* @var int101*/102public $tval;103104/**105* POP3 Username106* @var string107*/108public $username;109110/**111* POP3 Password112* @var string113*/114public $password;115116/////////////////////////////////////////////////117// PROPERTIES, PRIVATE AND PROTECTED118/////////////////////////////////////////////////119120private $pop_conn;121private $connected;122private $error; // Error log array123124/**125* Constructor, sets the initial values126* @access public127* @return POP3128*/129public function __construct() {130$this->pop_conn = 0;131$this->connected = false;132$this->error = null;133}134135/**136* Combination of public events - connect, login, disconnect137* @access public138* @param string $host139* @param integer $port140* @param integer $tval141* @param string $username142* @param string $password143*/144public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {145$this->host = $host;146147// If no port value is passed, retrieve it148if ($port == false) {149$this->port = $this->POP3_PORT;150} else {151$this->port = $port;152}153154// If no port value is passed, retrieve it155if ($tval == false) {156$this->tval = $this->POP3_TIMEOUT;157} else {158$this->tval = $tval;159}160161$this->do_debug = $debug_level;162$this->username = $username;163$this->password = $password;164165// Refresh the error log166$this->error = null;167168// Connect169$result = $this->Connect($this->host, $this->port, $this->tval);170171if ($result) {172$login_result = $this->Login($this->username, $this->password);173174if ($login_result) {175$this->Disconnect();176177return true;178}179180}181182// We need to disconnect regardless if the login succeeded183$this->Disconnect();184185return false;186}187188/**189* Connect to the POP3 server190* @access public191* @param string $host192* @param integer $port193* @param integer $tval194* @return boolean195*/196public function Connect ($host, $port = false, $tval = 30) {197// Are we already connected?198if ($this->connected) {199return true;200}201202/*203On Windows this will raise a PHP Warning error if the hostname doesn't exist.204Rather than supress it with @fsockopen, let's capture it cleanly instead205*/206207set_error_handler(array(&$this, 'catchWarning'));208209// Connect to the POP3 server210$this->pop_conn = fsockopen($host, // POP3 Host211$port, // Port #212$errno, // Error Number213$errstr, // Error Message214$tval); // Timeout (seconds)215216// Restore the error handler217restore_error_handler();218219// Does the Error Log now contain anything?220if ($this->error && $this->do_debug >= 1) {221$this->displayErrors();222}223224// Did we connect?225if ($this->pop_conn == false) {226// It would appear not...227$this->error = array(228'error' => "Failed to connect to server $host on port $port",229'errno' => $errno,230'errstr' => $errstr231);232233if ($this->do_debug >= 1) {234$this->displayErrors();235}236237return false;238}239240// Increase the stream time-out241242// Check for PHP 4.3.0 or later243if (version_compare(phpversion(), '5.0.0', 'ge')) {244stream_set_timeout($this->pop_conn, $tval, 0);245} else {246// Does not work on Windows247if (substr(PHP_OS, 0, 3) !== 'WIN') {248socket_set_timeout($this->pop_conn, $tval, 0);249}250}251252// Get the POP3 server response253$pop3_response = $this->getResponse();254255// Check for the +OK256if ($this->checkResponse($pop3_response)) {257// The connection is established and the POP3 server is talking258$this->connected = true;259return true;260}261262}263264/**265* Login to the POP3 server (does not support APOP yet)266* @access public267* @param string $username268* @param string $password269* @return boolean270*/271public function Login ($username = '', $password = '') {272if ($this->connected == false) {273$this->error = 'Not connected to POP3 server';274275if ($this->do_debug >= 1) {276$this->displayErrors();277}278}279280if (empty($username)) {281$username = $this->username;282}283284if (empty($password)) {285$password = $this->password;286}287288$pop_username = "USER $username" . $this->CRLF;289$pop_password = "PASS $password" . $this->CRLF;290291// Send the Username292$this->sendString($pop_username);293$pop3_response = $this->getResponse();294295if ($this->checkResponse($pop3_response)) {296// Send the Password297$this->sendString($pop_password);298$pop3_response = $this->getResponse();299300if ($this->checkResponse($pop3_response)) {301return true;302} else {303return false;304}305} else {306return false;307}308}309310/**311* Disconnect from the POP3 server312* @access public313*/314public function Disconnect () {315$this->sendString('QUIT');316317fclose($this->pop_conn);318}319320/////////////////////////////////////////////////321// Private Methods322/////////////////////////////////////////////////323324/**325* Get the socket response back.326* $size is the maximum number of bytes to retrieve327* @access private328* @param integer $size329* @return string330*/331private function getResponse ($size = 128) {332$pop3_response = fgets($this->pop_conn, $size);333334return $pop3_response;335}336337/**338* Send a string down the open socket connection to the POP3 server339* @access private340* @param string $string341* @return integer342*/343private function sendString ($string) {344$bytes_sent = fwrite($this->pop_conn, $string, strlen($string));345346return $bytes_sent;347}348349/**350* Checks the POP3 server response for +OK or -ERR351* @access private352* @param string $string353* @return boolean354*/355private function checkResponse ($string) {356if (substr($string, 0, 3) !== '+OK') {357$this->error = array(358'error' => "Server reported an error: $string",359'errno' => 0,360'errstr' => ''361);362363if ($this->do_debug >= 1) {364$this->displayErrors();365}366367return false;368} else {369return true;370}371372}373374/**375* If debug is enabled, display the error message array376* @access private377*/378private function displayErrors () {379echo '<pre>';380381foreach ($this->error as $single_error) {382print_r($single_error);383}384385echo '</pre>';386}387388/**389* Takes over from PHP for the socket warning handler390* @access private391* @param integer $errno392* @param string $errstr393* @param string $errfile394* @param integer $errline395*/396private function catchWarning ($errno, $errstr, $errfile, $errline) {397$this->error[] = array(398'error' => "Connecting to the POP3 server raised a PHP warning: ",399'errno' => $errno,400'errstr' => $errstr401);402}403404// End of class405}406?>407408