Heey helpers ik ben al een tijdje bezig met het uitwerken van een eigen mailing class...
Ik loop nu alleen stuk op mijn login het zal vast een klein foutje zijn maar ik kan hem niet vinden...
Hij moet evengoed zijn login doorlopen ook als de variable leeg zijn...
Alvast bedankt,
Michael
Output
220 *** ESMTP Exim 4.67 Tue, 13 Apr 2010 01:57:56 +0200
EHLO localhost
250-*** Hello localhost [127.0.0.1]
250-SIZE 20971520
250-ETRN
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
AUTH LOGIN
334 VXNlcm5hbWU6
334 UGFzc3dvcmQ6
535 Incorrect authentication data
Ik loop nu alleen stuk op mijn login het zal vast een klein foutje zijn maar ik kan hem niet vinden...
Hij moet evengoed zijn login doorlopen ook als de variable leeg zijn...
Alvast bedankt,
Michael
PHP:
<?php
Class Mail
{
public $debugMode = false;
private $smtpServer;
private $smtpPort;
private $smtpSocket;
private $authUser;
private $authPass;
//
// Basic constructor
//
public function __construct($Server=false, $Port=25)
{
//
$this->smtpServer = (($Server!==false) ? $Server : $_SERVER["LOCAL_ADDR"]);
$this->smtpPort = $Port;
}
public function connect($timeout=5)
{
$this->smtpSocket = @fsockopen($this->smtpServer, $this->smtpPort, $errno, $errstr, $timout);
//Check if connected
if ( !$this->smtpSocket )
{
$this->Debug("Cannot connect to the smtp server \r\n");
return false;
}
//Check if the buffer right
if( $this->Buffer() != 220 )
{
echo "Error Close Socket:";;
fclose($this->smtpSocket);
return false;
}
//Check if the login is right
if (($this->authUser == "" && $this->authPass == ""))
{
$cmd = sprintf("EHLO %s", "localhost");
} else {
$cmd = sprintf("HELO %s", "localhost");
}
$this->Command($cmd);
//Do Login
if ($this->Buffer() == 250)
{
return $this->authLogin();
}
if ( $this->Buffer() != 250)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
return false;
}
echo "Succesfully connected";
return true;
}
//
// Login to the smtp server
//
private function authLogin()
{
$cmd = "AUTH LOGIN";
$this->Command($cmd);
if($this->Buffer()!= 334)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
var_dump($this->Buffer());
return false;
}
$cmd=sprintf("%s",base64_encode($this->authUser));
$this->Command($cmd);
if($this->Buffer() != 334)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
return false;
}
$cmd=sprintf("%s",base64_encode($this->authPass));
$this->Command($cmd);
if($this->Buffer() != 235)
{
echo "Error Close Socket:";
fclose($this->smtpSocket);
return false;
}
return true;
}
//
// Send a new smtp command
//
private function Command($lstLines, $raw=0)
{
if ($raw){}
//Create array if its not created
if ( !is_array($lstLines) )
{
$lstLines = str_replace("\r", "", $lstLines);
$lstLines = explode("\n", $lstLines);
if ( !count($lstLines) )
{
$lstLines = Array($lstLines);
}
}
//Send the Command
foreach ($lstLines as $line)
{
$line = trim($line);
$this->Debug($line);
if ( !fputs($this->smtpSocket, $line."\r\n") )
{
return false;
}
}
return true;
}
//
// Check Buffer value
//
private function Buffer()
{
$line="";
while( !feof($this->smtpSocket) )
{
$ch = fgetc($this->smtpSocket);
if(!strlen($ch))
{
return false;
}
if($ch=="\n"){
$this->Debug($line,0);
if($line[3]==" ")
{
return (int)substr($line,0,3);
}
$line = ""; continue;
}
if($ch!="\r")
{
$line.=$ch;
}
}
return false;
}
//
// Debugger
//
private function Debug($err,$sent=1)
{
$err=trim($err);
if( !$this->debugMode )
{
return;
}
echo nl2br(htmlentities($err)."<br />");
}
}
?>
PHP:
<?php
require("Mail.php");
$smtp = new Mail("localhost",25);
$smtp -> debugMode=true;
if ( $smtp->connect() )
{
}
?>
Output
220 *** ESMTP Exim 4.67 Tue, 13 Apr 2010 01:57:56 +0200
EHLO localhost
250-*** Hello localhost [127.0.0.1]
250-SIZE 20971520
250-ETRN
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
AUTH LOGIN
334 VXNlcm5hbWU6
334 UGFzc3dvcmQ6
535 Incorrect authentication data