



SMTP класс:
- Код: Выделить всё • Развернуть
<?php
/*
SMTP Class v 1.7
*/
/**
* smtp class
*
*/
class BMSMTP
{
var $_host;
var $_port;
var $_sock;
var $_helo;
var $_my_host;
/**
* constructor
*
* @param string $host
* @param int $port
* @return BMSMTP
*/
function BMSMTP($host, $port, $my_host)
{
$this->_host = $host;
$this->_port = $port;
$this->_helo = false;
$this->_my_host = $my_host;
}
/**
* establish connection
*
* @return bool
*/
function Connect()
{
$this->_sock = @fsockopen($this->_host, $this->_port, $errNo, $errStr, SOCKET_TIMEOUT);
if(!is_resource($this->_sock))
{
PutLog(sprintf('SMTP connection to <%s:%d> failed (%d, %s)',
$this->_host,
$this->_port,
$errNo,
$errStr),
PRIO_WARNING,
__FILE__,
__LINE__);
return(false);
}
else
{
$responseLine = $this->_getResponse();
if(substr($responseLine, 0, 3) != '220')
{
PutLog(sprintf('SMTP server <%s:%d> did not return +OK',
$this->_host,
$this->_port),
PRIO_DEBUG,
__FILE__,
__LINE__);
return(false);
}
return(true);
}
}
/**
* log in
*
* @param string $user
* @param string $pass
* @return bool
*/
function Login($user, $pass)
{
fwrite($this->_sock, 'EHLO ' . $this->_my_host . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '250'
&& $this->_helo = true;
if(fwrite($this->_sock, 'AUTH LOGIN' . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '334')
{
if(fwrite($this->_sock, base64_encode($user) . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '334')
{
if(fwrite($this->_sock, base64_encode($pass) . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '235')
{
return(true);
}
else
{
PutLog(sprintf('SMTP server <%s:%d> rejected username or password for user <%s>',
$this->_host,
$this->_port,
$user),
PRIO_WARNING,
__FILE__,
__LINE__);
}
}
else
{
PutLog(sprintf('SMTP server <%s:%d> rejected username <%s>',
$this->_host,
$this->_port,
$user),
PRIO_WARNING,
__FILE__,
__LINE__);
}
}
else
{
PutLog(sprintf('SMTP server <%s:%d> does not seem to support LOGIN authentication',
$this->_host,
$this->_port,
$user),
PRIO_WARNING,
__FILE__,
__LINE__);
}
return(false);
}
/**
* disconnect
*
* @return bool
*/
function Disconnect()
{
fwrite($this->_sock, 'QUIT' . "\r\n")
&& $this->_getResponse();
fclose($this->_sock);
return(true);
}
/**
* initiate mail transfer
*
* @param string $from Sender address
* @param mixed $to Recipients (single address or array of addresses)
* @return bool
*/
function StartMail($from, $to)
{
// send helo, if not sent yet (e.g. at login)
if(!$this->_helo)
fwrite($this->_sock, 'HELO ' . $this->_my_host . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '250'
&& $this->_helo = true;
// send MAIL FROM
if(fwrite($this->_sock, 'MAIL FROM:<' . $from . '>' . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '250')
{
if(!is_array($to))
$to = array($to);
// send RCPT TO
foreach($to as $address)
fwrite($this->_sock, 'RCPT TO:<' . $address . '>' . "\r\n")
&& $this->_getResponse();
// ok!
return(true);
}
else
{
PutLog(sprintf('SMTP server <%s:%d> did not accept sender address <%s>',
$this->_host,
$this->_port,
$from),
PRIO_DEBUG,
__FILE__,
__LINE__);
}
return(false);
}
/**
* send mail data
*
* @param resource $fp File pointer
* @return bool
*/
function SendMail($fp)
{
// send DATA command
if(fwrite($this->_sock, 'DATA' . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '354')
{
// send mail
fseek($fp, 0, SEEK_SET);
while(!feof($fp)
&& ($line = fgets2($fp)) !== false
&& fwrite($this->_sock, rtrim($line) . "\r\n") !== false);
// finish
return(fwrite($this->_sock, "\r\n" . '.' . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '250');
}
return(false);
}
/**
* reset session
*
* @return bool
*/
function Reset()
{
return(fwrite($this->_sock, 'RSET' . "\r\n")
&& substr($this->_getResponse(), 0, 3) == '250');
}
/**
* get smtp server response (may consist of multiple lines)
*
* @return string
*/
function _getResponse()
{
$response = '';
while($line = fgets2($this->_sock))
{
$response .= $line;
if($line[3] != '-')
break;
}
return($response);
}
}
?>
POP3 Класс:
- Код: Выделить всё • Развернуть
<?php
/*
POP3 Class v 1.6
*/
/**
* pop3 access class
*
*/
class BMPOP3
{
var $_host;
var $_port;
var $_sock;
/**
* constructor
*
* @param string $host
* @param int $port
* @return BMPOP3
*/
function BMPOP3($host, $port)
{
$this->_host = $host;
$this->_port = $port;
}
/**
* establish connection
*
* @return bool
*/
function Connect()
{
$this->_sock = @fsockopen($this->_host, $this->_port, $errNo, $errStr, SOCKET_TIMEOUT);
if(!is_resource($this->_sock))
{
PutLog(sprintf('POP3 connection to <%s:%d> failed (%d, %s)',
$this->_host,
$this->_port,
$errNo,
$errStr),
PRIO_DEBUG,
__FILE__,
__LINE__);
return(false);
}
else
{
$responseLine = fgets2($this->_sock);
if(substr($responseLine, 0, 1) != '+')
{
PutLog(sprintf('POP3 server <%s:%d> did not return +OK',
$this->_host,
$this->_port),
PRIO_DEBUG,
__FILE__,
__LINE__);
return(false);
}
return(true);
}
}
/**
* log in
*
* @param string $user
* @param string $pass
* @return bool
*/
function Login($user, $pass)
{
if(fwrite($this->_sock, 'USER ' . $user . "\r\n")
&& substr(fgets2($this->_sock), 0, 1) == '+')
{
if(fwrite($this->_sock, 'PASS ' . $pass . "\r\n")
&& substr(fgets2($this->_sock), 0, 1) == '+')
{
return(true);
}
else
{
PutLog(sprintf('POP3 server <%s:%d> rejected password for user <%s>',
$this->_host,
$this->_port,
$user),
PRIO_DEBUG,
__FILE__,
__LINE__);
}
}
else
{
PutLog(sprintf('POP3 server <%s:%d> rejected username <%s>',
$this->_host,
$this->_port,
$user),
PRIO_DEBUG,
__FILE__,
__LINE__);
}
return(false);
}
/**
* disconnect
*
* @return bool
*/
function Disconnect()
{
fwrite($this->_sock, 'QUIT' . "\r\n")
&& fgets2($this->_sock);
fclose($this->_sock);
return(true);
}
/**
* get list of mails
*
* @return array
*/
function GetMailList()
{
$result = array();
$msgNum = $msgSize = $msgUID = 0;
// LIST command -> get msg numbers and sizes
if(fwrite($this->_sock, 'LIST' . "\r\n")
&& substr(fgets2($this->_sock), 0, 1) == '+')
{
while(($line = trim(fgets2($this->_sock))) != '.'
&& $line != '')
{
if(sscanf($line, '%d %d', $msgNum, $msgSize) == 2)
{
$result[$msgNum] = array(
'num' => $msgNum,
'size' => $msgSize,
'uid' => false
);
}
}
// try to get UIDs
if(fwrite($this->_sock, 'UIDL' . "\r\n")
&& substr(fgets2($this->_sock), 0, 1) == '+')
{
while(($line = trim(fgets2($this->_sock))) != '.'
&& $line != '')
{
if(sscanf($line, '%d %s', $msgNum, $msgUID) == 2)
if(isset($result[$msgNum]))
$result[$msgNum]['uid'] = $msgUID;
}
}
return($result);
}
else
{
PutLog(sprintf('LIST command at POP3 server <%s:%d> failed',
$this->_host,
$this->_port),
PRIO_DEBUG,
__FILE__,
__LINE__);
return(false);
}
}
/**
* retrieve mail to file pointer
*
* @param int $num Message number
* @param resource $fp File pointer
* @return bool
*/
function RetrieveMail($num, $fp)
{
if(fwrite($this->_sock, 'RETR ' . (int)$num . "\r\n")
&& substr(fgets2($this->_sock), 0, 1) == '+')
{
$oldPos = ftell($fp);
while(($line = fgets2($this->_sock))
&& !(substr($line, 0, 1) == '.' && trim($line) == '.'))
{
$line = rtrim($line) . "\r\n";
fwrite($fp, $line);
}
fseek($fp, $oldPos, SEEK_SET);
return(true);
}
return(false);
}
/**
* mark mail for deletion
*
* @param int $num Message number
* @return bool
*/
function DeleteMail($num)
{
return(fwrite($this->_sock, 'DELE ' . (int)$num . "\r\n")
&& substr(fgets2($this->_sock), 0, 1) == '+');
}
}
?>