PHP为从数据库中选择的多个用户发送邮件

So I'm using PHP like this:

if(isset($userID)) {    

$premium = $con->prepare("
SELECT Email
FROM tblName as d       
WHERE Rank = $rank and Type = $type
"); 
$premium->execute();

$premium->bind_result($email);


} else {
    echo "There is no User ID detected, try to refresh browser.";   
}

while ($premium->fetch()) { 

    # SUBJECT (Subscribe/Remove)
     $subject = "New Resume";

    # RESULT PAGE
    $location = "http://www.website.com";

    $sender = "info@website.com";

    # MAIL BODY
    $message = '<html><body>';    
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
    $message .= "</table>";
    $message .= "</body></html>";

    $cc = "ss@gmail.com";
    $headers = "From: " . $sender . "
";
    $headers = "BCC: " . $cc . "
";
    $headers .= "Reply-To: ". strip_tags($_POST['Email']) . "
";
    $headers .= "MIME-Version: 1.0
";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1
";

    $to = $email;

    mail( $to, $subject, $message, $headers) or die ("Mail could not be sent.");
}
header("Location: http://website.com/");
die(); 

mysqli_close($link);

But this not sending email for each selected users. How correctly I could use loop? Should I use foreach or improve while loop? How could I apply with arrays in this case? Could someone get me on correct way? Thank you!

I have achieved this using foreach loops.

Before using foreach loops save all email address in a single array and iterate over the array to reduce runtime.

You could go the other way round by iterating over the whole data gotten from database.

Your choice.

You have to use the fetch_assoc() in the while since if you use that it will fetch all the record one by one and then it will allow to send MAIL as per the requirement that you need.

And you have to concatenate the header in the second line over here because it will overwrite the statements

Replace:

$headers = "From: " . $sender . "
";
$headers = "BCC: " . $cc . "
";

with:

$headers = "From: " . $sender . "
";
$headers .= "BCC: " . $cc . "
";

As you are using BCC dont use loop to send mail. loop over your query result collect all recipients id and send mail in one call.

BCC: blind carbon copy to tertiary recipients who receive the message. The primary and secondary recipients cannot see the tertiary recipients. Depending on email software, the tertiary recipients may only see their own email address in BCC, or they may see the email addresses of all primary and secondary recipients.

Prepared statements should take advantage of placeholders to safely assign variables prior to execution

As was pointed out in a comment - if you assign each recipient in the BCC field you can collect the email addresses in the loop but send the email after the loop. This way each recipient will see the TO address but none of the other recipients.

if( isset( $userID, $rank, $type, $_POST['Email'] ) ) {  

    /* create sql with placholders and prepare */
    $sql='select email from tblname where rank=? and type=?';
    $premium = $con->prepare( $sql );

    /* Only proceed if the prepared statement succeeded */
    if( $premium ){

        /* bind the variables to the placeholders and execute */
        $premium->bind_param('ss',$rank,$type);
        $premium->execute();
        $premium->bind_result( $email );



        $to = "ss@gmail.com";
        $bcc= array( $to );
        $subject = "New Resume";
        $location = "http://www.website.com";
        $sender = "info@website.com";   

        /* collect all email addresses and add to BCC */
        while( $premium->fetch() ) {
            $bcc[]=$email;
        }

        /* close the prepared statement */
        $premium->close();

        /* Close the db connection */
        $link->close();


        $message = '
        <html>
            <body>
                <table rules="all" style="border-color: #666;" cellpadding="10"></table>
            </body>
        </html>';

        $headers = "From: " . $sender . "
";
        $headers .= "BCC: " . implode( ',', $bcc ) . "
";
        $headers .= "Reply-To: ". strip_tags( $_POST['Email'] ) . "
";
        $headers .= "MIME-Version: 1.0
";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1
";



        $status=@mail( $to, $subject, $message, $headers );




        die( header( "Location: " . ( $status ? 'http://website.com/?mailsent=true' : 'http://website.com/?mailsent=false' ) ) ); 
    } 
} else {
    echo "There is no User ID detected, try to refresh browser.";   
}