Smtp Auth

Status
Niet open voor verdere reacties.

slabbetje

Gebruiker
Lid geworden
5 mei 2007
Berichten
290
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

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
 
eerst en vooral denk ik dat er geen spatie mag staan op plaats
5$smtp->debugMode=true;
dacht ik zo direct.


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));
        
        //>>>>>>>>>>>>>>>>>>>>>>moet hier $cmd niet veranderen<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        
        
        $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
     *@param $err debugmsg
     *@param $sent [optional] true/false
     *@return null
     */
    private function Debug($err,$sent=1){
        $err=trim($err);
        if( !$this->debugMode ){
            return;
        }
         echo nl2br(htmlentities($err)."<br />");
    }
}

?>
Dit is eentje waar ik aan bezig was maar nog niet volledig af
PHP:
<?php	
 	class Amail{
 		private $headers=array();
 		private $issend;
 		function __construct($from){
 			if($this->is_spammer($from)){
 				$this->headers["From:"] = "web<$from>";
 				$this->headers["Content-Type"] = "text/plain; charset='iso-8859-1'";
 				//$this->headers["MIME-Version"] = "1.0";
 				//$this->headers["Content-Transfer-Encoding"] = "8Bit";
 				$this->headers["X-Mailer"] = "PHP/".phpversion();
 				$this->headers["Reply-To:"]= "$from";
 			}else{
			   //$this=null;//kill myself
 			}
 		}
 		/**
 		* @param $naam   naam verzender
 		* @param $email  email adress
 		* @return true/false
 		* @walkit
 		* !important! do not remake watch to array_walk
 		*/
		function send($naam,$email){
			$this->issend = mail($email,$this->subject,$this->html,$this->headers);
			return $this->issend;
		}
		/**
 		* @maillist 
 		* asiosiatieve array (name => mail)
 		*/
		function maillist($array){
			(is_array ($array))? array_walk($array, 'send'):null;			
		}
		/**
		 * @param $key  vb: "From:"  ,"Reply-To:";
 		 * @return true/false
		*/
		public function setheader($key,$value){
			$this->headers[$key] =$value;
		}
		public function sethtml(){
			$this->headers["Content-Type:"] = " text/html; charset='iso-8859-1'";
		}
		private function getheaders(){
			return implode(" ",$this->headers);
		}
		private function getheaderstest(){
			array_walk($array, 'wrap_each');
			$array_csv = implode(" ", $array);
		}
		function wrap_each(&$item){
    		$item = "'$item'";
		}
		private function is_spammer($from){
			return $this->is_headerinject($from);
		}
		private function is_headerinject($from){
			//%0D%0A HTML-equivalent =>    \r\n
			return pos($var,"%0D%0A")||pos($var,"%0D%0Abcc:");
		}
	}
?>
 
Een spatie voor/na een verwijzing van een variable of functie is geen fout en word gewoon correct uitgelezen...
Ik leg hierboven ook uit dat hij stukloopt op zijn login :p
De debugMode variable word namelijk alleen gebruikt in de functie Debug
En deze functie zorgt voor de output en die heb ik gewoon

Verder zie ik dat jij gebruik maakt van de standaard mail() functie van php en ik maak gebruik van een socket

Edit
Zag dat je het scriptje had aangepast...
Alleen er staat een comment bij dat ik $cmd moet aanpassen maar dat doe ik toch al;)

$this->authUser
$this->authPass
 
Laatst bewerkt:
heb je bovenste script lijn 70 gezien Ik dacht te zien dat daar mogelijks iets van paswoord commando zou moeten maar ik weet te weinig van socket om het u te zeggen.tja het is voor experts
dit is code die ik ooit eens op internet vondt ik hoop dat je er iets mee bent
vergeet vooral bij debug de @ weg te halen ik laat ze staan zodat je weet waar ze stonden
PHP:
       if ($this->bind_host){
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_bind($socket,$this->bind_host);
            if (!@socket_connect($socket,$this->remote_host,$this->remote_port)){
                $OK = FALSE;
            }
        }else{
            $socket = @fsockopen( $this->remote_host, $this->remote_port, $sock_errno, $sock_errstr, 10 );
        }
        if ( !$socket || !$OK ){
            $this->error[] = "Can't create socket connection to $this->remote_host:$this->remote_port.";
            return 0;
        }
        // if we have a username and password, add the header
        if ( isset($this->remote_uname) && isset($this->remote_passwd) ){
            $array_headers['Authorization'] = 'Basic '.base64_encode("$this->remote_uname:$this->remote_passwd");
        }
        // for DA skins: if $this->remote_passwd is NULL, try to use the login key system
        if ( isset($this->remote_uname) && $this->remote_passwd == NULL ){
            $array_headers['Cookie'] = "session={$_SERVER['SESSION_ID']}; key={$_SERVER['SESSION_KEY']}";
        }
 
Dankje voor je hulp maar ik ben er denk ik al uit..
Ik lees net dat mijn webhost geen wachtwoord voor smtp omdat je via localhost zit

Dit is allemaal geen probleem alleen dit was wel het gevolg waarom mijn socket sluit...
Hier moet ik dus nog een kleine check voor schrijven en dan doet hij het

Edit: Ik heb nu een connectie en geen error dus gaat helemaal goed
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan