Loop Sending Emails with PHP

Asked

Viewed 450 times

0

Guys this is my first post and I would love your help: I have a query in a database that sends automatic email through PHP Mailer if the activity is delayed, so far so good, the problem is in repetition, if you have 3 emails to send is sent to the first ok, then the second 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 from the third activity (which was not to receive). Could someone give me a light? Follow 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();
}


?>
  • There is a syntax error here: $cliente->EMAIL_RESPONSAVEL;,

  • I think here you are adding all customer foreach emails to receive the msg. // Here I take the BD email $mail->Addaddress($client->EMAIL_RESPONSAVEL;, 'Delayed Activity Test'); You need to clear old addresses or create a new email.

  • I did so to make it less polluted and to know if it was sent: foreach($customers as $customer): $email = $client->EMAIL_RESPONSAVEL; $mail->Addaddress($email, 'Delayed Activity Test'); $mail->Msghtml($client->SUBJECT); $mail->Send(); echo "Message successfully sent to '.$client->SUBJECT'. </p> n"; echo "Message successfully sent to '.$client->EMAIL_RESPONSAVEL'. </p> n"; Sleep(10); endforeach; .

  • Call $mail->clearAddresses() after sending

  • bfavaretto It worked, I stopped receiving 3 times (or more) in the first email, but I only receive the first two, for example: Activity 1 goes pro gmail, activities 2 and 3 go to the Hotmail, but in the Hotmail only reaches activity 2.... activity 3 gets lost in the middle of the road. What can be?

  • Thanks everyone for their help. bfavaretto tip worked perfectly. I put $mail->clearAddresses() after sending and it worked. Thank you!

  • If you want to send messages to multiple recipients exclusively, using the same instance of the Phpmailer object, in your loop, before calling Addaddress(), you should call the Clearaddresses method().

Show 2 more comments
No answers

Browser other questions tagged

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