我正在尝试根据从我的数据库中提取的特定结果发送电子邮件。我试图在我的foreach
循环中执行while
语句,为每个结果发送一封电子邮件,但即使我知道应该有更多结果,我只会得到一个结果而不是一个。
这是我对数据库和我的while
循环的调用:
//get USER info
$getTenant = $db->query("SELECT user_fname, user_lname, user_email FROM users WHERE user_id={$user_id}");
$tenant = $getTenant->fetch_array();
$user_fname = $tenant['user_fname'];
$user_lname = $tenant['user_lname'];
$user_email = $tenant['user_email'];
// SELECT statement for RENTALS.
$getRentals = $db->query("SELECT rental_id, user_id, unit_id, rental_renewdate, rental_balance FROM rentals");
// Search each result to send email
while($rental = $getRentals->fetch_assoc()) {
$rental_id = $rental['rental_id'];
$user_id = $rental['user_id'];
$unit_id = $rental['unit_id'];
$rental_renewdate = strtotime($rental['rental_renewdate']);
$rental_balance = $rental['rental_balance'];
以下是我尝试创建foreach
语句,以便为每一行提取电子邮件:
if ($today == $notify_prior) {
// SELECT statement for EMAILS for PRIOR.
$getPrior = $db->query("SELECT email_subject, email_h1, email_body, email_fromemail FROM emails WHERE email_id=1");
$prior = $getPrior->fetch_array();
$email_subject = $prior['email_subject'];
$email_h1 = $prior['email_h1'];
$email_body = $prior['email_body'];
$email_fromemail = $prior['email_fromemail'];
$getPrior->close();
foreach ($getRentals as $prior_warnings){
$prior_warnings[] = array($rental_id, $user_fname, $user_lname, $user_email);
/* PRIOR NOTIFICATION CUSTOMER EMAIL*/
$to = '$user_email';
$subject = $email_subject;
$headers = 'Content-type: text/html; charset=iso-8859-1\r\n';
$headers .= 'From: '. $email_fromemail . '\r\n' . 'Reply-To: '. $email_fromemail . '\r\n' . 'X-Mailer: PHP/' . phpversion();
$message = '<p>'. $user_fname .','. $user_lname .'</p>
<h1>'. $email_h1 .'</h1>
<p>'. str_replace(array("{UNIT_NUMBER}","{DUE_DATE}"), array($unit['unit_number'],date('m-d-Y',$rental_renewdate)), stripslashes($email_body)) .'</p>';
mail($to, $subject, $message, $headers);
}
}
}
我似乎唯一能做的就是创建一个无限循环,为一个人发送大量电子邮件。