Jump to content


luizj's Content

There have been 1 items by luizj (Search limited from 29/03/2023)


Ordernar por                Order  

#1000635 [Classe Smtp] Alguem Pode Deixa-La Mais Rapida?

Posted by luizj on 22/06/2010, 16:17 in PHP

smtp.class.php
<?
//Script feito por Luiz Nobrega
class Smtp
{
    var $conn;
    var $host  = "????";
    var $from  = "???@???";
    var $user  = "???@???";
    var $pass  = "???";
    var $debug = false;

    function Send($to, $subject, $msg)
    {
        // Boas Vindas
        if($this->Get(3) != "220") return false;
        $this->Put("EHLO ".$this->host);
        while(1)
        {
            $str = fgets($this->conn, 512);
            if($str == "250 HELP")break;
        }

        // Autenticação
        $this->Put("AUTH LOGIN");
        if($this->Get(3) != "334") return false;
        $this->Put(base64_encode($this->user));
        if($this->Get(3) != "334") return false;
        $this->Put(base64_encode($this->pass));
        if($this->Get(3) != "235") return false;
        
        // Header e Mensangem
        $this->Put("MAIL FROM: <".$this->from.">");
        if($this->Get(3) != "250") return false;
        $this->Put("RCPT TO: <".$to.">");
        if($this->Get(3) != "250") return false;
        $this->Put("DATA");
        if($this->Get(3) != "354") return false;
        $this->Put($this->toHeader($to, $subject));
        $this->Put($msg);
        $this->Put(".");
        if($this->Get(3) != "250") return false;
        
        // Desconexão
        $this->Put("QUIT");
        if($this->Get(3) != "221") return false;
        
        fclose($this->conn);
        if(isset($this->conn)){
            return true;
        }else{
            return false;
        }
    }

    function Put($value)
    {
        if($this->debug)echo "<b>>>></b> ".$value."<br>";
        return fputs($this->conn, $value . "\x0D\x0A");
    }
    
    function Get($len)
    {
        $inn = fgets($this->conn, 1024);
        if($this->debug)echo "<b><<<</b> ".$inn."<br>";
        return substr($inn, 0, 3);
    }

    function toHeader($to, $subject)
    {
        $headers = "Message-Id: <". date('YmdHis').".". md5(microtime()).".". strtoupper($this->from) .">\r\n";
        $headers .= "From: ".$this->from."\r\n";
        $headers .= "To: ".$to."\r\n";
        $headers .= "Subject: ".$subject."\r\n";
        $headers .= "Date: ". date('D, d M Y H:i:s O') ."\r\n";
        $headers .= "X-MSMail-Priority: High\r\n";
        $headers .= "Content-Type: Text/HTML";
        return $headers;
    }
}

function envia_email($to, $subject, $msg)
{
    $enviar_via_mail = false;
    
    $smtp = new Smtp();
    $smtp->conn = @fsockopen($smtp->host, 25, $errno, $errstr, 10);
    if(!$smtp->conn)
    {
        $enviar_via_mail = true;
    }else{
        if(!$smtp->Send($to, $subject, $msg))$enviar_via_mail = true;
    }
    
    if($enviar_via_mail == true)
    {
        $headers = "From: <".$smtp->from.">\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Date: ". date('D, d M Y H:i:s O') ."\r\n";
        $headers .= "X-MSMail-Priority: High\r\n";
        $headers .= "X-mailer: PHP Script\r\n";
        $headers .= "Content-Type: Text/HTML";
        @mail($to, $subject, $msg, $headers);
    }
}
?>

Eu uso um servidor de emails dedicado, configurado por min mesmo, consegui fazer ele receber meu pedido de envio de email direto pelo php sem passar pelo comando mail(), somente em caso de falha.
Da tudo certo, porem o script esta meio lento na parte do EHLO, se alguem puder me ajudar a melhorar seu desempenho ou me mostrar um melhor eu fico agradecido, obrigado.




IPB Skin By Virteq