Namespace Address

0

I have the file below that usually sends authenticated emails :

<?php            

        ini_set("display_errors",true);
        ini_set("display_startup_erros",1);

        error_reporting(E_ALL && E_NOTICE);
    error_reporting( E_ALL | E_STRICT ); // PHP 5.3
    error_reporting( E_ALL ); // Todas as outras versões 

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP;

    require_once 'PHPMailer/PHPMailer.php';
    require_once 'PHPMailer/Exception.php';
    require_once 'PHPMailer/SMTP.php';

    require_once '_global/_erros/erros.ini';
    require_once '_controles/_util/Constantes.php';

    $constantes = new Constantes();

    $caixaPostalServidorNome=$constantes->getTituloSite();
    $caixaPostalServidorEmail= $constantes->getEmailSite();
    $caixaPostalServidorSenha=$constantes->getSenhaEmailSite();

    $email = "[email protected]";   
    $nome = "Carlos";
    $assunto =  "Testando...."; 
    $mensagem = "Indo bem";

    $mail = new PHPMailer(true);
     //Server settings
    $mail->SMTPDebug = 2;               
    $mail->isSMTP();                   
    $mail->Host = 'smtp.'.$constantes->getDominioSite();
    $mail->SMTPAuth = true;   
    $mail->Username  = $caixaPostalServidorEmail;
    $mail->Password  = $caixaPostalServidorSenha;
    $mail->SMTPSecure = 'TLS';               
    $mail->Port = 587;                                 

    //Recipients      
    $mail->From  = $caixaPostalServidorEmail;
    $mail->FromName  = utf8_decode($caixaPostalServidorNome); 
    $mail->Subject  = utf8_decode($assunto);
    $mail->Body  = utf8_decode($mensagem);


    $mail->AddAddress($email,utf8_decode($nome));

    if ($mail->send()) {

        $_SESSION["success"] = "Mensagem enviada com sucesso";

    } else {

        $_SESSION["danger"] = "Erro ao enviar mensagem " . $mail->ErrorInfo;

    }
?>

So, I decided to put the $mail part of a class and call it from a file and it looks like this: In time: This was necessary because the above code was made from an example and without the validations etc ...

<?php                

        ini_set("display_errors",true);
        ini_set("display_startup_erros",1);
        error_reporting(E_ALL && E_NOTICE);
    error_reporting( E_ALL | E_STRICT ); // PHP 5.3
    error_reporting( E_ALL ); // Todas as outras versões 

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP;   

    require_once "PHPMailer/PHPMailer.php";     
    require_once "PHPMailer/Exception.php";     
    require_once "PHPMailer/SMTP.php";

    require_once "_controles/_conexao/Conexao.php";
        require_once "_controles/_util/Constantes.php";
    require_once "_controles/_util/PhpUtil.php";    
    require_once "_controles/_models/Emails.php";
    require_once "_controles/_daos/EmailsDao.php";
    require_once "_controles/_models/EmailEnviar.php";
    require_once "_controles/_daos/EmailEnviarDao.php";

    $connection = new Conexao(); 
    $conexao = $connection->abreConexao();
        $constantes = new Constantes();  
        $phpUtil = new PhpUtil();   

    $_POST["nome"] = "Carlos";
    $_POST["email"] = "[email protected]";
    $_POST["telefone"] = "";
    $_POST["assunto"] = 2;
    $_POST["descricao"] = "Tentei né?";

        $assunto = $phpUtil->retornaContatoTipos($_POST["assunto"]);    
    $emailsDao = new EmailsDao($conexao);

    $qual = isset($_POST["qual"]) ? $_POST["qual"] : "";

    $_POST["telefone"] = preg_replace( '#[^0-9]#', '', $_POST["telefone"] );


    if (
          strlen($_POST["telefone"]) > 0 && 
          strlen($_POST["telefone"]) != 10 && 
          strlen($_POST["telefone"]) != 11) {

        echo "ERRO";

    } else {

        $email = new Emails(
                         date("Y-m-d"), 
                     "n", 
                     $_POST["nome"], 
                     $_POST["email"], 
                     $_POST["telefone"],
                     $_POST["assunto"], 
                     $_POST["descricao"]);

        $email->setQual($qual);


        $emailsDao->cadastrar($email);
        // Apenas para popular o texto abaixo
        $assunto = $_POST["assunto"] == 4 ? $_POST["qual"] : $phpUtil->retornaContatoTipos($_POST["assunto"]);  

        $texto  = "<h2>".$constantes->getTituloSite()."</h2><br />";
        $texto .= "<img style='display:block; margin:0 auto;' src='".$constantes->getHttpSite()."/_img/logo.png' />";
        $texto .= "<b>Olá, você nos enviou um e-mail com a seguinte mensagem:</b><br /><br />";
        $texto .= "<b>Nome:</b> ".$_POST["nome"]."<br /><br />";
        $texto .= "<b>Telefone:</b> ".$phpUtil->formataTel($_POST["telefone"])."<br /><br />";
        $texto .= "<b>E-mail:</b> ".$_POST["email"]."<br /><br />";
        $texto .= "<b>Interesse:</b> ".$assunto."<br /><br />";
        $texto .= "<b>Descrição:</b><br />".nl2br($_POST["descricao"])."<br /><br /><br />";
        $texto .= "Estaremos respondendo o mais rápido possível<br /><br />";

        $emailEnviar = new EmailEnviar( 
              $_POST["nome"], 
              $_POST["email"],
              $constantes->getTituloSite(), 
              "contato@".$constantes->getDominioSite(), 
              "Re: ".$assunto,
              $texto
        );




        $emailEnviarDao = new EmailEnviarDao();

        $enviarEmail = $emailEnviarDao->enviaEmail($emailEnviar, $constantes);

        echo $enviarEmail["success"] == 1 ? "OK" : "ERRO";

    }

?>

Until the line below everything goes well:

$enviarEmail = $emailEnviarDao->enviaEmail($emailEnviar, $constantes);

But when you enter the class I created for submission:

<?php            

  class EmailEnviarDao {

    public function __construct() {}

    public function enviaEmail($email, $constantes)  {

       $caixaPostalServidorNome=$constantes->getTituloSite();
       $caixaPostalServidorEmail= $constantes->getEmailSite();
       $caixaPostalServidorSenha= $constantes->getSenhaEmailSite();
       $host = 'smtp.'.$constantes->getDominioSite();

       $enviaFormularioParaNome = $email->getNomeAlvo();
       $enviaFormularioParaEmail = $email->getEmailAlvo();    
       $assunto =  $email->getAssunto();      
       $mensagem = $email->getDescricao();

       $mail = new PHPMailer(true);

       //Server settings
       $mail->SMTPDebug = 2;               
       $mail->isSMTP();                    
       $mail->Host = $host;
       $mail->SMTPAuth = true;   
       $mail->Username  = $caixaPostalServidorEmail;
       $mail->Password  = $caixaPostalServidorSenha;
       $mail->SMTPSecure = 'TLS';               
       $mail->Port = 587;                                 

        //Recipients      
       $mail->From  = $caixaPostalServidorEmail;
       $mail->FromName  = utf8_decode($caixaPostalServidorNome); 
       $mail->Subject  = utf8_decode($assunto);
       $mail->Body  = utf8_decode($mensagem);

       $mail->AddAddress($enviaFormularioParaEmail,utf8_decode($enviaFormularioParaNome));

       if($mail->Send()){

            return array("success"=>1,"errors"=>"0K");

       } else {

            return array("success"=>0,"errors"=>$mail->ErrorInfo);

       } 

    }   

  }

?>

Then I get an error but I can not debug.

Here is the error:

Fatal error: Uncaught Error: Class 'PHPMailer' not found in C:\Program Files\Apache24\Apache24\htdocs\funerariasaopedro.net.br\_controles\_daos\EmailEnviarDao.php:19 Stack trace: #0 C:\Program Files\Apache24\Apache24\htdocs\funerariasaopedro.net.br\enviar.php(93): EmailEnviarDao->enviaEmail(Object(EmailEnviar), Object(Constantes)) #1 {main} thrown in C:\Program Files\Apache24\Apache24\htdocs\funerariasaopedro.net.br\_controles\_daos\EmailEnviarDao.php on line 19

It looks like a class addressing error within the other but I can not do it right and understand.

I ask you for help

It can be viewed at link

    
asked by anonymous 29.05.2018 / 15:34

1 answer

1

Good afternoon, in your EmailEnviarDao.php file on line 19 you are instantiating the PHPMailer object, well to do this before you need:

  • Include class, with require, include or etc.
  • Use the namespace ( use PHPMailer\PHPMailer\PHPMailer etc) or when instantiating inform the full NS path.
  • As you are not doing this in this file, it arrives at line 19 and the interpreter does not have the PHPMailer class definition and thus generates the fatal error.

    Hint, use a better IDE because the IDE itself at line 19 should already / should be indicating to you that the class is not available.

        
    30.05.2018 / 21:44