Link Nacional
Por favor Login or Registre

Base de Conhecimento

Enviando e-mail autenticado com PHP

Olá saiba como enviar email por PHP de forma segura e eficiente...


Abaixo segue a Classe de envio em PHP:

Abra um Arquivo Novo e salve como: smtp.class.php

DICA: uma boa prática é salvar este arquivo em uma pasta especifica tipo /classe/smtp.class.php

<?
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; charset=utf-8";
return $header;
}
function Close(){
$this->Put("QUIT");
if($this->debug == true){
while (!feof ($this->conn)) {
fgets($this->conn) . "<br>\n";
}
}
return fclose($this->conn);
}
}

?>



Abra um novo arquivo em PHP e salve como contato.php:


<?php include ("classe/smtp.class.php"); // inserir a classe dentro do arquivo ?>

<?php
$envio = false;
/* Configuração da classe.smtp.php */
$host = "localhost"; /*host do servidor SMTP na Link Nacional use sempre localhost */
$smtp = new Smtp($host);
$smtp->user = "sememail@seudominio.com.br";/*conta de email para envio do email.*/
$smtp->pass = "digitesuasenha";/* senha dousuario do servidor SMTP*/
$smtp->debug =true; /* ativar a autenticação SMTP, se for da Link Nacional deixe true*/
$to = "voureceber@nodominio.com.br"/*email q irá receber as infos*/
?>


<?php if($_POST['id_form'] == "contato" $_POST['nome'] != "" && $_POST['telefone'] != "" && $_POST['mensagem'] != "" && $_POST['correio'] != ""){ // verifica se todos os campos foram habilitador e confirma o id do form

$msg ="<br />E-mail: ".$_POST['correio'];
$msg .="<br />Nome: ".$_POST['nome'];
$msg .="<br />Telefone: ".$_POST['telefone'];
$msg .="<br />Mensagem: ".$_POST['mensagem'];


/* envia uma mensagem */
$from= $_POST['correio']; /* seu e-mail */

$subject = "ASSUNTO: Formulário de Contato"; /* assunto da mensagem */
$smtp->Send($to, $from, $subject, $msg);/* faz o envio da mensagem */

$envio = true;// confirmação de envio
}
?>

<?php if($envio){ ?>

Sua Mensagem foi enviada com sucesso!

<?php }else{ ?>

<form id="form1" name="form1" method="post" action="contato.php">
                        <table>
                            <tr>
                                <td align="right">Nome:</td>
                                <td><label>
                                  <input type="text" name="nome" id="nome" />
                                  <input name="id_form" type="hidden" id="id_form" value="contato" />
                                </label></td>
                            </tr>
                           
                            <tr>
                                <td align="right">E-mail:</td>
                                <td><label>
                                  <input type="text" name="correio" id="correio" />
                                </label></td>
                            </tr>
                           
                            <tr>
                                <td align="right">Telefone:</td>
                                <td><label>
                                  <input type="text" name="telefone" id="telefone" />
                                </label></td>
                            </tr>
                           
                            <tr>
                                <td valign="top" align="right">Mensagem:</td>
                              <td><label>
                              <textarea name="mensagem" id="mensagem" cols="45" rows="5"></textarea>
                              </label></td>
                            </tr>
                           
                             <tr>
                              <td></td>
                               <td><label>
                                    <input type="submit" name="button" id="button" value="Enviar mensagem"  />
                               </label></td>
                            </tr>
                           
                        </table>  
                   </form><?php } ?>



Esta resposta lhe foi útil?

Adicionar aos Favoritos Adicionar aos Favoritos

Imprimir este Artigo Imprimir este Artigo


Powered by WHMCompleteSolution

Idioma:

Menu Fácil

Login Cliente

Pesquisar