简体   繁体   中英

Issue in While Loop sending email for many users

Im trying to when I click in my sendForm button, I want to send an email for all of my active users.

I have this code below, and I have a While loop to send an email for every emails that I have in my condition (status = active) .

But the email is only sending for my first email in the table and not for every mail that I have.

Somebody there see why this is happening?

if(isset($_POST['sendForm']))
{
    $verifyUser= $pdo->prepare("SELECT * FROM users WHERE status = ?");  
    $verifyUser->bindValue(1, 'active');
    $verifyUser->execute();
    $verifyUserRows= $verifyUser->rowCount();

    if($verifyUserRows<= '0')
    {
    echo 'there are no active warnings'; 
    }

  else
  {
    while ($verifyUserResult= $verifyUser->fetch(PDO::FETCH_ASSOC)) 
    {
    $date = date('d/m/Y H:i');
    $msg = " 

    Hi, this is my message!

    Send at $data
    ";

    sendMail('My subject',$msg,MAILUSER,$verifyUserResult['email']);
    echo 'Email sent with sucess'; 
    return;
    }
}

The issue is that your return; statement is within your while loop.

The return statement in this particular loop will cause the loop to terminate. In this case, you have placed it as the last statement of the loop and so it will process your first email to send out, then terminate.

To fix the problem, remove the return; statement completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM