Loop of sending emails with PHP

0

Personal this is my first post and I would like your help: I have a database query that sends automatic mail through PHP Mailer in case the activity is delayed, so far so good, the problem is in the repetition , if you have 3 emails to send you are sent to the first one ok, then the second one receives ok (but the first email tb receives data from the second that was not to receive) and finally the first email receives the data of the third activity was to receive). Could someone give me a light? Here is the code:

require 'conexao.php';

date_default_timezone_set('America/Sao_Paulo');
$hoje = date('Y-m-d H:i');

$conexao = conexao::getInstance();
$sql = 'SELECT ASSUNTO, PRAZO, RESPONSAVEL_GERAL, EMAIL_RESPONSAVEL FROM agenda WHERE PRAZO < :data';
$stm = $conexao->prepare($sql);
$stm->bindValue(':data', $hoje);
$stm->execute();
$clientes = $stm->fetchAll(PDO::FETCH_OBJ);

    require_once("class/class.phpmailer.php");

    $mail = new PHPMailer(true);

    $mail->IsSMTP();




try {
     $mail->Host = 'smtp.meuhost.com.br'; 
     $mail->SMTPAuth   = true;  
     $mail->Port       = 587; 
     $mail->Username = '[email protected]';
     $mail->Password = 'minha_senha';

     $mail->SetFrom('[email protected]', 'Agenda OnLine'); 
     $mail->Subject = 'Lembrete de Atividades em Atraso';


        foreach($clientes as $cliente):

       // Aqui pego o email do BD
        $mail->AddAddress($cliente->EMAIL_RESPONSAVEL;, 'Teste de Atividades em Atraso');
         // E aqui pego o assunto
             $mail->MsgHTML($cliente->ASSUNTO); 

             $mail->Send();
             echo "Mensagem enviada com sucesso para '.$cliente->EMAIL_RESPONSAVEL'.</p>\n";

             $email = '';

        sleep(10);

        endforeach;


    }catch (phpmailerException $e) {
      echo $e->errorMessage();
}


?>
    
asked by anonymous 17.10.2017 / 15:06

0 answers