Source for file class.mail_fetch.php
Documentation is available at class.mail_fetch.php
* Class depends on PHP pcre extension and fsockopen() function. Some features
* might require PHP 4.3.0 with OpenSSL or PHP 5.1.0+. Class checks those extra
* dependencies internally, if used function needs it.
* @copyright © 2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: class.mail_fetch.php,v 1.2 2006/08/26 17:33:58 tokul Exp $
* @link http://www.ietf.org/rfc/rfc1939.txt RFC1939
* @link http://www.ietf.org/rfc/rfc2449.txt POP3EXT
* @link http://www.ietf.org/rfc/rfc2595.txt STARTTLS
* @link http://www.ietf.org/rfc/rfc1734.txt AUTH command (unsupported)
* POP connection is opened when class is constructed. All command_* methods
* execute specific POP commands on server. Most of other methods should be
* used only internally. Only login() method is public. If command returns
* mixed content and you expect message text, ids or something else, make sure
* that it is not boolean false.
* 1. create object with connection params, see mail_fetch method.
* 3. login($username,$password) - true = login successful, false = login error.
* 4. command_stat() - get number of messages
* 5. command_list() - get message ids, use command_uidl(), if you implement
* 'keep mess on server' functions. Make sure that you handle possible UIDL
* 6. command_retr($some_message_id) - get message contents
* 7. command_dele($some_message_id) - mark message for deletion
* 8. command_quit() - close connection. You must close connection in order
* to delete messages and remove mailbox lock.
* Defaults to 110 on plain text connections and to 995 on TLS
* 0 - plain text (default)
* 1 - tls (php 4.3 and openssl extension requirement)
* 2 - stls (stream_socket_enable_crypto() requirement. PHP 5.1.0, POP3
* server with POP3EXT and STLS support)
* Bitwise variable. If variable covers more than one authentication method,
* login() tries to use all of them until first successful auth.
* 1 - user/pass (rfc1939, default)
* 2 - apop (rfc1939, timestamp must be present in greeting)
* Timestamp (with <> or empty string)
* Capabilities (POP3EXT capa)
* Variable is used to store last positive POP server response
* checked in check_response() method. Used internally to handle
* mixed single and multiline command responses.
* 'host' - required string, address of server. ip or fqn
* 'port' - optional integer, port of server.
* 'tls' - optional integer, connection type
* 'timeout' - optional integer, connection timeout
* 'auth' - optional integer, used authentication mechanism.
* See description of class properties
* @param array $aParams connection params
if (isset
($aParams['host'])) {
$this->host =
$aParams['host'];
return $this->set_error('Server name is not set');
if (isset
($aParams['tls'])) {
$this->tls = (int)
$aParams['tls'];
if (isset
($aParams['port'])) {
$this->port = (int)
$aParams['port'];
if (isset
($aParams['timeout'])) {
$this->timeout = (int)
$aParams['timeout'];
if (isset
($aParams['auth'])) {
$this->auth = (int)
$aParams['auth'];
// Generic methods to handle connection and login operations.
* Command handles TLS and STLS connection differences and fills capabilities
* array with RFC2449 CAPA data.
return $this->set_error('Used PHP version does not support functions required for POP TLS.');
$target =
'tls://' .
$this->host;
$error =
sprintf('Error %d: ',$errno) .
$errstr;
// check greeting for errors and extract timestamp
* fill capability only when connection uses some non-rfc1939
* authentication type (currently unsupported) or STARTTLS.
* Command is not part of rfc1939 and we don't have to use it
* in simple POP connection.
if ($this->auth >
3 ||
$this->tls===
2) {
* Reads first response line and checks it for errors
* @return boolean true = success, false = failure, check error buffer
* Standard SquirrelMail function copied to class in order to make class
* independent from SquirrelMail.
* Connection is not closed on login error (unless POP server drops
* @param string $username
* @param string $password
function login($username,$password) {
// RFC1939 APOP authentication
if (! $ret &&
$this->auth & 2) {
// RFC1939 USER authentication
if (! $ret &&
$this->auth & 1) {
// Default to USER/PASS login
// error is already in error buffer
* Sets error in error buffer and returns boolean false
* @param string $error Error message
* @param boolean $close_conn Do we have to close connection
function set_error($error,$close_conn=
false) {
// POP (rfc 1939) commands
* array with 'count' and 'size' keys
* @return mixed array or boolean false
if (preg_match('/^\+OK (\d+) (\d+)/',$response,$matches)) {
return array('count' =>
$matches[1],
return $this->set_error('stat command failed');
* @return mixed array with message ids (keys) and sizes (values) or boolean false
// add space between command and msg_id
if(!empty($msg)) $msg =
' ' .
$msg;
* @param integer $msg message id
* @return mixed rfc822 message (CRLF line endings) or boolean false
// Optional RFC1939 commands
* Gets message headers and $n of body lines.
* Command is optional and not required by rfc1939
* @return string or boolean false
* Gets unique message ids
* Command is optional and not required by rfc1939
* @param integer $msg message id
* @return mixed array with message ids (keys) and unique ids (values)
//return $this->set_error('Unsupported command.');
// add space between command and msg_id
if(!empty($msg)) $msg =
' ' .
$msg;
$ids[$msg_id] =
"$unique_id";
// make sure that unique_id is a string.
$ids[$msg_id] =
"$unique_id";
* USER authentication (username command)
* Command is optional and not required by rfc1939. If command
* is successful, pass command must be executed after it.
* @param string $username
* @return boolean true = success, false = failure.
* USER authentication (password command)
* Command is optional and not required by rfc1939. Requires
* successful user command.
* @param string $password
* @return boolean true = success, false = failure.
* Command is optional and not required by rfc1939. APOP support
* requires plain text passwords stored on server and some servers
* don't support it. Standard qmail pop3d declares apop support
* without checking if checkpassword supports it.
* @param string $username
* @param string $password
* @return boolean true = success, false = failure.
return $this->set_error('APOP is not supported by selected server.');
* Checks pop server capabilities
* RFC2449. Fills capabilities array.
// reset array. capabilities depend on authorization state
// if capa fails, error buffer contains error message.
// if POP3EXT is not supported, capability array will be empty
* RFC 2595 POP STARTTLS support
return $this->set_error('Used PHP version does not support functions required for POP STLS.',true);
return $this->set_error('Selected POP3 server does not support STLS.',true);
if (@stream_socket_enable_crypto($this->conn,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
// starttls was successful (rfc2595 4. POP3 STARTTLS extension.)
/** stream_socket_enable_crypto() call failed. */
return $this->set_error('Unable to start TLS.',true);
Documentation generated on Sat, 07 Oct 2006 16:09:05 +0300 by phpDocumentor 1.3.0RC6