所以我正在使用这样的PHP:
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 . "\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);
但是这不会为每个选定的用户发送电子邮件。我怎么能正确使用循环?我应该使用foreach
还是改进while
循环?在这种情况下,我如何申请arrays
?有人能帮我正确吗?谢谢!
答案 0 :(得分:1)
当你使用BCC时,不要使用循环来发送邮件。 循环查询结果收集所有收件人ID并在一次调用中发送邮件。
BCC:向接收该消息的第三位接收者进行盲目抄送。主要和次要收件人无法看到第三位收件人。根据电子邮件软件,第三方收件人只能在BCC中看到自己的电子邮件地址,或者他们可能会看到所有主要和次要收件人的电子邮件地址。
答案 1 :(得分:0)
我使用foreach循环实现了这一点。
在使用foreach循环之前,将所有电子邮件地址保存在单个数组中并迭代数组以减少运行时间。
你可以通过迭代从数据库获得的整个数据来反过来。
您的选择。
答案 2 :(得分:0)
您必须在while中使用fetch_assoc()
,因为如果您使用它,它将逐个获取所有记录,然后它将允许根据您需要发送MAIL。
你必须在这里连接第二行的标题,因为它会覆盖语句
<强>替换强>
$headers = "From: " . $sender . "\r\n";
$headers = "BCC: " . $cc . "\r\n";
<强>与强>
$headers = "From: " . $sender . "\r\n";
$headers .= "BCC: " . $cc . "\r\n";
答案 3 :(得分:0)
准备好的语句应该利用占位符在执行前安全地分配变量
正如评论中指出的那样 - 如果您在BCC字段中分配每个收件人,您可以在循环中收集电子邮件地址,但在循环后发送电子邮件。这样,每个收件人都会看到TO
地址,但没有其他收件人。
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 . "\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.";
}