douyan1453 2016-10-10 06:56
浏览 104

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!

  • 写回答

4条回答 默认 最新

  • dongmei9203 2016-10-10 07:00
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么