<?php

#################################
#                               #
# SmtpMailer v1.0               #
# Developed by: rEd nEcK *      #
#                               #
#################################

class SmtpMailer{
  var $sock, $host, $port, $error;
  var $isAuth, $user, $pass;
  function SmtpMailer($host,$port=25,$isAuth=true){
    $this->host = $host;
    $this->port = $port;
    $this->isAuth = $isAuth;

    $this->sock = fsockopen($host, $port, $errno, $errstr, 30);
    if( $this->Get() <> 220 ) $error = "Falha ao tentar se conectar ao servidor";

    $this->Put("EHLO $host");
    if( $this->Get() <> 250 ) $error = "Falha ao tentar enviar o comando EHLO";
  }
  function Auth(){
    $this->Put("AUTH LOGIN");
    if( $this->Get() <> 250 ) $error = "Falha ao tentar enviar o comando AUTH PLAIN";

    $this->Put(base64_encode($this->user));
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar o USUARIO";

    $this->Put(base64_encode($this->pass));
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar a SENHA";

    if( $this->Get() <> 235 ) $error = "Falha ao tentar autentificar"; 
  }
  function MakeHeader($to, $from, $subject){
    $header  = "MIME-Version: 1.0\r\n";

    $header .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $header .= "Message-Id: <". date('YmdHis').".". md5(microtime()).".". strtoupper($from) ."> \r\n";
  
    $header .= "From: <" . $from . "> \r\n";
    $header .= "To: <".$to."> \r\n";

    $header .= "Subject: ".$subject." \r\n";
    $header .= "Date: ". date('D, d M Y H:i:s O') ." \r\n";

    $header .= "X-MSMail-Priority: High \r\n";
    return $header;
  }
  function Send($to, $from, $subject, $msg){
    if( $this->isAuth == true )
      $this->Auth();

    $this->Put("MAIL FROM: " . $from);
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar o comando MAIL FROM";

    $this->Put("RCPT TO: " . $to);
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar o comando RCPT TO";

    $this->Put("DATA");
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar o comando DATA";

    $this->Put($this->MakeHeader($to, $from, $subject));
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar as HEADERS";

    $this->Put("\r\n");
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar o comando NEWLINE";

    $this->Put($msg);
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar a MENSAGEM";

    $this->Put(".");
    if( $this->Get() <> 334 ) $error = "Falha ao tentar enviar a SENHA";

    if( $this->Get() <> 250 ) return false; else return true;
    $this->Close();
  }
  function Close(){
    $this->Put("QUIT");
    return fclose($this->sock);
  }
  function Put($value){
    return fputs($this->sock, $value . "\r\n");
  }
  function Get(){
    $line = fgets($this->sock). "<br>\n";
    return $line{0}.$line{1}.$line{2};
  }
}

define('SMTP_HOST','mail.host.com');     // host do servidor smtp
define('SMTP_PORT',25);                    // porta do servidor smtp
define('SMTP_USER','user@host.com');  // usuario para autentifica��o
define('SMTP_PASS','****');            // senha para autentifica��o

$sendVar["destino"]  = "contato@host.com";
$sendVar["assunto"]  = "Mensagem enviada via Smtp";
$sendVar["mensagem"] = "Bla<b>bla</b>bla<br>blublu";

$smtp = new SmtpMailer(SMTP_HOST,SMTP_PORT,true);
$smtp->user = SMTP_USER;
$smtp->pass = SMTP_PASS;
$smtp->Send($sendVar["destino"], SMTP_USER, $sendVar["assunto"], $sendVar["mensagem"]);
 
?>