class SmtpMail
{
// declare class variables
private $host;
private $port;
private $sock;
private $log;
/**
* Constructor - Is called when the class is instanced
*
* @access: public
* @param Str $host
* @param Int $port
* @return NONE
*/
public function __construct($host='localhost', $port=25)
{
// set server-variables
$this->host = $host;
$this->port = $port;
}
/**
* connect() - Connects to the given smtp-server
*
* @access: public
* @return Boolean
*/
public function connect()
{
// control-connection handle is saved to $handle
$this->sock = @fsockopen($this->host, $this->port);
if ( !$this->sock OR !$this->check('220') )
throw new Exception("Connection failed.");
// switch to non-blocking mode - just return data no response
set_socket_blocking($this->sock, true);
// set timeout of the server connection
stream_set_timeout($this->sock, 0, 200000);
return true;
}
/**
* ehlo() - Sends greeting to secured server
*
* @access: public
* @param Str $user
* @param Str $pwd
* @return Boolean
*/
public function ehlo($user, $pwd)
{
// send EHLO -spezified in RFC 2554
$this->cmd("EHLO " . $this->host);
if( !$this->check('250') )
throw new Exception("Failed to send EHLO.");
// send authentification-identifier
$this->cmd("AUTH LOGIN");
if( !$this->check('334') )
throw new Exception("Failed to send AUTH.");
// send user-name
$this->cmd(base64_encode("HS2847_system"));
if( !$this->check('334') )
throw new Exception("Failed to send user-name.");
// send password
$this->cmd(base64_encode("Jellybo1"));
if( !$this->check('235') )
throw new Exception("Failed to send password.");
return true;
}
/**
* helo() - Sends greeting to server
*
* @access: public
* @return Boolean
*/
public function helo()
{
// Send the RFC821 specified HELO.
$this->cmd('HELO ' . $this->host);
if( !$this->check('250') )
throw new Exception("Failed to send HELO.");
return true;
}
/**
* from() - Sends specified addressor
*
* @access: public
* @param Str $from
* @return Boolean
*/
public function from($from)
{
// specify addressor
$this->cmd("MAIL FROM: $from");
if( !$this->check('250') )
throw new Exception("Failed to send addressor.");
return true;
}
/**
* rcpt() - Sends specified acceptor
*
* @access: public
* @param Str $to
* @return Boolean
*/
public function rcpt($to)
{
// send specified acceptor
$this->cmd("RCPT TO: $to");
if( !$this->check('250') )
throw new Exception("Failed to send acceptor.");
return true;
}
/**
* data() - Sends the data to the server
*
* @access: public
* @param Str $message
* @param Arr $header
* @return NONE
*/
public function data($message, $header)
{
// initiate data-transfere
$this->cmd('DATA');
if( !$this->check('354') )
throw new Exception("Data-transfere failed.");
// validate header-data
if( !is_array($header) )
throw new Exception("Header-data must be an array.");
// initiate counter
$i = 0;
// include header data
foreach( $header as $key => $value)
{
// send header
if( $i < count($header)-1 )
{
$this->cmd("$key: $value");
}
else
{
$this->cmd("$key: $value\r\n");
}
$i++;
}
// send the message
$this->cmd("$message\r\n");
// send end parameter
$this->cmd('.');
$this->check('250');
}
/**
* quit() - Closes the server-connection
*
* @access: public
* @return NONE
*/
public function quit()
{
$this->cmd("QUIT");
$this->check('221');
fclose($this->sock);
return true;
}
/**
* cmd() - Sets a ftp-command given by the user
*
* @access: public
* @param Str $cmd
* @return NONE
*/
public function cmd($cmd)
{
fputs($this->sock, "$cmd\r\n");
$this->log("> $cmd");
}
/**
* getReply() - Gets the reply of the ftp-server
*
* @access: public
* @return String
*/
public function getReply()
{
$go = true;
$message = "";
do
{
$tmp = @fgets($this->sock, 1024);
if($tmp === false)
{
$go = false;
}
else
{
$message .= $tmp;
if( preg_match('/^([0-9]{3})(-(.*[\r\n]{1,2})+\\1)? [^\r\n]+[\r\n]{1,2}$/', $message) ) $go = false;
}
} while($go);
$this->log($message);
return $message;
}
/**
* checkControl() - Checks if the response of a command is ok
*
* @access: public
* @param Str $reply
* @return Boolean
*/
public function valid()
{
// get response of the server
$this->response = $this->getReply();
// check the response and say if everything is allright
return (empty($this->response) || preg_match('/^[5]/', $this->response)) ? false : true;
}
/**
* check() - Checks if the response-code is correct
*
* @access: public
* @param Str $code
* @return Boolean
*/
public function check($code)
{
if($this->valid())
{
$pat = '/^'. $code .'/';
if( preg_match($pat, $this->response))
{
return true;
}
}
return false;
}
/**
* log() - Saves all request to the server and their responses into $this->log
*
* @access: private
* @return NONE
*/
private function log($str)
{
$this->log .= "$str
";
}
/**
* getLog() - Prints out all requests to the server and their responses
*
* @access: public
* @return NONE
*/
public function getLog()
{
return $this->log;
}
}
?>