Reputation: 898
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 = "[email protected]";
# MAIL BODY
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "</table>";
$message .= "</body></html>";
$cc = "[email protected]";
$headers = "From: " . $sender . "\r\n";
$headers = "BCC: " . $cc . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['Email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$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!
Upvotes: 0
Views: 1216
Reputation: 33813
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 = "[email protected]";
$bcc= array( $to );
$subject = "New Resume";
$location = "http://www.website.com";
$sender = "[email protected]";
/* 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 . "\r\n";
$headers .= "BCC: " . implode( ',', $bcc ) . "\r\n";
$headers .= "Reply-To: ". strip_tags( $_POST['Email'] ) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$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.";
}
Upvotes: 0
Reputation: 6936
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.
Upvotes: 1
Reputation: 4210
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 . "\r\n";
$headers = "BCC: " . $cc . "\r\n";
with:
$headers = "From: " . $sender . "\r\n";
$headers .= "BCC: " . $cc . "\r\n";
Upvotes: 0
Reputation: 358
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.
Upvotes: 0