Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/12/2009, 23:28
Avatar de pato12
pato12
 
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 17 años, 4 meses
Puntos: 101
Problema al conectar con servidor smtp

Hola,
Trato de conectarme al servidor smtp de gmail, pero no lo logro. Este es mi codigo:
Código PHP:
Ver original
  1. <?php
  2. class ForoSmtp{
  3.     private $data=array(
  4.                         'autentificar'=>false,
  5.                         'host'=>'',
  6.                         'port'=>25,
  7.                         'ssl'=>false,
  8.                         'user'=>array(
  9.                                       'user'=>'',
  10.                                       'clave'=>''
  11.                                       ),
  12.                         'conn'=>false,
  13.                         'destinos'=>array(),
  14.                         'mensaje'=>array('html'=>false,'msg'=>''),
  15.                         'titulo'=>'',
  16.                         'server'=>''
  17.                         );
  18.     public $Foro;
  19.    
  20.     public function __construct($Foro){
  21.     //  $this->Foro=$Foro;
  22.     }
  23.     public function setHost($host){
  24.         $this->data['host']=$host;
  25.     }
  26.     public function setPort($port){
  27.         $this->data['port']=$port;
  28.     }
  29.     public function setAuth($user=false,$clave=false){
  30.         if(!$user||!$clave){
  31.         //  $this->Foro->setError('Error, no se definio el usuario o clave.');
  32.         }
  33.         $this->data['autentificar']=true;
  34.         $this->data['user']['user']=$user;
  35.         $this->data['user']['clave']=$clave;
  36.     }
  37.     public function isHtml($html=false){
  38.         $this->data['mensaje']['html']=$html;
  39.     }
  40.     public function isSsl($ssl){
  41.         $this->data['ssl']=$ssl;
  42.     }
  43.     public function setMsg($msg){
  44.         $this->data['mensaje']['msg']=$msg;
  45.     }
  46.     public function setDestino($email,$nombre=''){
  47.         $this->data['destinos'][]=array($email,$nombre);
  48.     }
  49.     public function setTitle($titulo){
  50.         $this->data['titulo']=$titulo;
  51.     }
  52.    
  53.     #######################################################
  54.    
  55.     public function SmtpConect(){
  56.         if(!$this->Conectar())
  57.             return false;
  58.         // hasta aqui se conecto todo bien, pero desde esta linea anda mal :S
  59.         if(!$this->SendHelo($this->data['server']))
  60.             return false;
  61.         return true;
  62. /*      if($this->data['autentificar']){
  63.             fputs($this->data['conn'],"AUTH LOGIN \r\n");
  64.             if(intval(substr($this->getRequest(),0,3))!=334)
  65.                 return false;
  66.            
  67.             fputs($this->data['conn'],base64_encode($this->data['user']['user'])."\r\n");
  68.             if(intval(substr($this->getRequest(),0,3))!=334)
  69.                 return false;
  70.                
  71.             fputs($this->data['conn'], base64_encode($this->data['user']['clave'])."\r\n");
  72.             if(intval(substr($this->getRequest(),0,3))!=235)
  73.                 return false;
  74.            
  75.         }*/
  76.         return true;
  77.     }
  78.     private function Conectar(){
  79.         $host=($this->data['ssl']?'ssl://':'').$this->data['host'];
  80.         $port=intval($this->data['port']);
  81.        
  82.         $con=@fsockopen($host,$port,$e,$em,30);
  83.         if(!$con)
  84.             return false;
  85.         $this->data['conn']=$con;
  86.         return true;
  87.     }
  88.     private function SendHelo($server){
  89.         if($this->data['conn']===false)
  90.             return false;
  91.            
  92.         if(empty($server)&&isset($_SERVER['SERVER_NAME']))
  93.             $server=$_SERVER['SERVER_NAME'];
  94.         else
  95.             $server="localhost.localdomain";
  96.            
  97.         if(!$this->resivirConexion('EHLO',$server)){
  98.             if(!$this->resivirConexion('HELO',$server))
  99.                 return false;
  100.         }
  101.         return true;
  102.     }
  103.     private function resivirConexion($helo,$server){
  104.         if($this->data['conn']===false)
  105.             return false;
  106.        
  107.         fputs($this->data['conn'],$helo." ".$server."\r\n");
  108.        
  109.         $buffer=$this->getRequest();
  110.         $code=substr($buffer,0,3);
  111.         if($code!=250)
  112.             return false;
  113.         return true;
  114.     }
  115. // igual al de phpmailer.. creo que esto es el problema :s
  116.     private function getRequest() {
  117.         if($this->data['conn']===false)
  118.             return false;  
  119.         $data="";
  120.         while($str=@fgets($this->data['conn'],515)){
  121.             $data.=$str;
  122.             if(substr($str,3,1)==" ")
  123.                 break;
  124.         }
  125.         return $data;
  126.     }
  127. }
  128. $ForoSmtp=new ForoSmtp('');
  129. $ForoSmtp->setHost('smtp.gmail.com');
  130. $ForoSmtp->setPort(465);
  131. $ForoSmtp->setAuth('[email protected]','miclave');
  132. if(!$ForoSmtp->SmtpConect())
  133.     exit('No se pudo conectar con el servidor.');
  134. echo "Conectado";
  135. ?>
Gracias
Salu2
__________________
Half Music - www.halfmusic.com