PHPMAILER library sending duplicate emails

Asked

Viewed 162 times

-2

I have a PHP code that sends data to Phpmailer (data resulting from a query in the database).

The problem is that when Phpmailer fires the email, it arrives duplicated in the inbox of who should receive the email.

public function editarChamados()
{   
  try 
  {
      $c = new Conexao();
      $db = $c->conectar();                                 

      if($this->descricao <> "")
      {                                 
          $descricao = "Olá";
            
        $query = "START TRANSACTION;
        SET @responsavel := (SELECT usuario FROM usuarios_ti WHERE id = :responsavel_ti);
        UPDATE chamados SET id_usuario_ti = :responsavel_ti, titulo = :titulo WHERE id = :id;
        INSERT INTO dialogo_chamados(id_chamado,dialogo,remetente)VALUE(:id,'$descricao',@responsavel);                                     
      COMMIT;
      ";

      $stmt = $db->prepare($query);
      $stmt->bindValue(":responsavel_ti",$this->responsavel_ti);    
      $stmt->bindValue(":titulo",$this->titulo);                        
      $stmt->bindValue(":id",$this->id);    

      if($stmt->execute())
      {
      //Chamando método que envia o email
      $this->enviarEmail();
      echo "sucesso";
      die();                            
      }
      else
      {
      print_r($stmt->errorInfo());
      }


      }                                                 

  } 
  catch(PDOException $e) 
  {
    echo "Erro: ".$e->getCode()." Mensagem: ".$e->getMessage();             
  }
}

//Método de envio de email
public function enviarEmail()
{

    $c = new Conexao();
    $db = $c->conectar();

    $query = "SELECT a.dialogo, a.remetente, a.data_dialogo, b.solicitante FROM dialogo_chamados AS a 
    INNER JOIN chamados AS b ON b.id = a.id_chamado WHERE b.id = :id ORDER BY a.data_dialogo";

    $stmt = $db->prepare($query);
    $stmt->bindValue(":id",$this->id);                  
    $stmt->execute();           

    $dialogos = $stmt->fetchAll(PDO::FETCH_ASSOC);


    require 'PHPMailer/src/Exception.php';
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';

    $mailer = new PHPMailer();

    try
    {

        $nome_remetente = $this->nome_responsavel_ti;
        $email_remetente = $this->email_responsavel_ti;
        $email_destinatario = $this->email_solicitante;
        $titulo_chamado = $this->titulo;
        $data_abertura_chamado = $this->data_abertura_chamado;

        $mailer->setLanguage('br');
        $mailer->CharSet = "utf8";
        $mailer->IsSMTP();
        //$mailer->SMTPDebug = 4;
        $mailer->SMTPAuth = true;
        $mailer->SMTPSecure = "tls";
        $mailer->Host = "smtp.skymail.net.br";
        $mailer->Port = 587;
        $mailer->Username = '[email protected]';
        $mailer->Password = "54h54fg6j4g6j";
        $mailer->Priority = 1;
        $mailer->addReplyTo($email_remetente, $nome_remetente);
        $mailer->setFrom('[email protected]', "JCA - Suporte Tecnico");
        $mailer->AddAddress($email_destinatario);
        $mailer->IsHTML(true);
        $mailer->Subject = 'Mensagem do Suporte Tecnico';
      
        $mensagem = "CONTEÚDO DO EMAIL";
     
        $mailer->Body = $mensagem;

        if($mailer->Send())
        { 
          $mailer->ClearAllRecipients();
          $mailer->ClearAttachments();
        } 
        else 
        { 
          echo "Mailer Error: " . $mailer->ErrorInfo;
          die();
        }   

    } 
    catch(PDOException $e) 
    {
       echo "Erro: ".$e->getCode()." Mensagem: ".$e->getMessage();
    }                           
}

- Caixa de entrada do Email:

  • @hkotsudo, how do I post PHP codes here on Stackoverflow ? I can never post correctly, because it seems that here there is not a suitable place to post PHP codes. I could, for example, post the code on a Pastebin and put the link in the question description ?

  • The ideal is that the code is always in the question and links serve only as a complement (if the original code is too large, try to reduce it to a [mcve]). Anyway, the code of this question is ok, I don’t know what’s wrong for you to think it’s not posted correctly (it just doesn’t run directly on the page, but this functionality is restricted to Javascript, HTML and CSS)

  • Can you tell if there is any redirection in the recipient’s email?

  • Then remember to change the password of this email '[email protected]'

  • @hkotsubo I say regarding organization, indentation , etc... Always when ppsto the codes here, they come out pretty disorganized. They don’t come out as they are in my text editor.

  • @Thiagocosta , is not this.

  • @Thiagocosta , does not exist.

  • @Gambi I do not know, I always put codes here and never had problems with formatting. The most I can suggest is to read the help center: https://answall.com/editing-help#code

  • @hkotsubo , where you post php code here ?

  • It worked, I added $Mailer->Clearaddresses(); inside if($Mailer->send()). Now I need to know why this happened. But the problem has been solved.

  • I always put the code in my own questions/answers. Sometimes when it’s interesting to show code running, I put it in some online IDE (like ideone.com, repl.it, etc.), but always as an add-on - if the code is essential to answer the question, then it should always be here on the site itself (as I have already said, external links should only be complements)

  • @hkotsubo , thank you!

  • Now it’s all clear. The little guy just changed PHPMAILER by Phpmailer in the question issue. If so, Stackoverflow will become a "sea of edits".

  • ... Worse than people are negatively questioning the question, but they don’t know how useful the information contained in the answer is and also how boring it is to go through the problem reported in the question. But it’s worth it!

Show 9 more comments

1 answer

1


  • I solved the problem using the PHPMAILER method, ClearAddresses().
  • I have been reading and I saw that this method is used to delete the destination addresses that were previously passed in the method AddAddress().

The part of the code I changed was like this:

if($mailer->Send())
{ 
    $mailer->ClearAllRecipients();
    $mailer->ClearAttachments();
    $mailer->ClearAddresses(); 
} 

Reference: Programacion.net

Browser other questions tagged

You are not signed in. Login or sign up in order to post.