Envio de email autenticado via SMTP
Fala pessoal, beleza?
estou desenvolvendo um script para enviar newsletter, e ando lendo muita coisa para fazer o envio dos emails.
Um camarada me disse que fazendo um envio via SMTP e não pelo mail() do php eu poderia até saber os erros de envio quando retornar algum problema.
Eis que vou atrás então de saber como fazer isso. Encontro este script de exemplo na internet:
<?
class Smtp{
var $conn;
var $user;
var $pass;
var $debug;
function Smtp($host){
$this->conn = fsockopen($host, 25, $errno, $errstr, 30);
$this->Put("EHLO $host");
}
function Auth(){
$this->Put("AUTH LOGIN");
$this->Put(base64_encode($this->user));
$this->Put(base64_encode($this->pass));
}
function Send($to, $from, $subject, $msg){
$this->Auth();
$this->Put("MAIL FROM: " . $from);
$this->Put("RCPT TO: " . $to);
$this->Put("DATA");
$this->Put($this->toHeader($to, $from, $subject));
$this->Put("\r\n");
$this->Put($msg);
$this->Put(".");
$this->Close();
if (isset($this->conn)) {
return true;
} else {
return false;
}
}
function Put($value){
return fputs($this->conn, $value . "\r\n");
}
function toHeader($to, $from, $subject){
$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";
$header .= "Content-Type: Text/HTML";
return $header;
}
function Close(){
$this->Put("QUIT");
if($this->debug == true){
while (!feof ($this->conn)) {
fgets($this->conn) . "<br>\n";
}
}
return fclose($this->conn);
}
}
?>
E aqui o arquivo para fazer o envio:
include ("teste2.php");
$host = "IP DO SERVIDOR AQUI";
$smtp = new Smtp($host);
$smtp->user = "meu@email.com.br";
$smtp->pass = "SENHA AQUI";
$smtp->debug =true;
$from = "email@de.com.br";
$to = "email@para.com";
$subject = "Teste de e-mail utilizando SMTP";
$msg = "Você está recebendo esta mensagem de teste<br>";
$msg .= "Para confirma clique no link abaixo";
$smtp->Send($to, $from, $subject, $msg);
Dúvidas:
1) Alguém vê algo errado?
2) Vocês acham mais fácil mesmo fazer o envio desta forma?
3) Ao executar o script estou recebendo essa msg de erro: Fatal error: Maximum execution time of 30 seconds exceeded
Valeu!
Discussão (4)
Carregando comentários...