我有以下功能,假設發送電子郵件給每個用戶。我相信在我的foreach循環中,我需要創建一個新的foreach循環來遍歷每封電子郵件,然後向每個用戶發送一次電子郵件,我該怎麼做?使用foreach循環遍歷數組
function getCronCuponDescargado(){
$app = JFactory::getApplication();
$db = JFactory::getDBO();
$query = 'SELECT k2i.title, count(cs.item_id) as contador, cs.user_id, u.email
FROM #__k2_items as k2i
LEFT JOIN #__cuphoneo_subscripcion as cs ON cs.item_id = k2i.id
LEFT JOIN #__users as u ON u.id = cs.user_id
WHERE cs.estado = 1 GROUP BY cs.item_id ORDER BY cs.user_id';
$db->setQuery($query);
$result = $db->loadObjectList();
$query = 'SELECT count(item_id)
FROM #__cuphoneo_subscripcion
WHERE estado = 0';
$db->setQuery($query);
$resultado = $db->loadObjectList();
$mailer =& JFactory::getMailer();
$mailer->IsHTML(true);
$emailSubject = '¡Tus Cuphones!';
$sender = $mailer->From;
$fcontent = '<h2>Listado de cupones a los que estas suscrito:</h2>
<table>
<tr>
<th>Nombre del cupón</th>
<th>Tiempo restante de cupón</th>
</tr>';
foreach($result as $value){
$correoUser = $value->email;
$idUser = $value->user_id;
//Loop through array using foreach to send one email to each different user
$correos = array($correoUser => $idUser);
var_dump($correos);
$mailer->setSender($sender);
$mailer->addRecipient($correos);
$mailer->setSubject($emailSubject);
$fcontent .= '<tr>
<td>'.$value->title.'</td>
<td>'.$value->id.'</td>
<td>'.$value->contador.'</td>
</tr>';
}
$fcontent .= '</table>';
$mailer->setBody($fcontent);
$send =& $mailer->Send();
}
$correos
具有以下值:
array(1) { ["[email protected]"]=> string(3) "118" } array(1) { ["[email protected]"]=> string(3) "119" }
看起來你並不需要'$西班牙郵政=陣列($ correoUser => $ ID用戶);'可言的,你應該改變'$ mailer-> addRecipient($西班牙郵政);''來$ mailer-> addRecipient($ correoUser);' – James