Je me permets de poster car je souhaiterai envoyer un mail et boucler pour que le mail soit envoyer une liste de diffusion.
Mais il ne l'envoi uniquement à la première personne
Voici mon code pour la liste de diffusion :
$liste_diff = [];
// - Liste de diffusion
if(isset($_POST['list']) && $_POST['list']){
$rs_list = $conn->prepare('SELECT DISTINCT c.mail FROM compte c
INNER JOIN tl_compte_foyer cf ON cf.idx_compte = c.id_compte
INNER JOIN utilisateur u ON c.id_compte = u.idx_compte
INNER JOIN tl_utilisateur_foyer uf ON uf.idx_utilisateur = u.id_utilisateur
WHERE cf.idx_foyer = ? AND uf.mailing = 1');
$rs_list->bindParam(1,$_SESSION['foyer']);
$rs_list->execute();
foreach( $rs_list as $list){
array_push($liste_diff,$list[0]);
}
}
if(isset($_POST['address']) && $_POST['address'] != ""){
foreach ($address as $add){
array_push($liste_diff,$add);
}
}
// - Liste Admin
if(isset($_POST['admin']) && $_POST['admin']){
$rs_admin = $conn->prepare('SELECT DISTINCT c.mail FROM xxx c
INNER JOIN xxx cf ON cf.xx = c.xx
INNER JOIN xx u ON c.xx = u.xx
INNER JOIN xx uf ON uf.xx = u.xx
WHERE cf.xx = ? AND uf.admin = 1
');
$rs_admin->bindParam(1,$_SESSION['foyer']);
$rs_admin->execute();
// $rs_admin->fetch();
foreach( $rs_admin as $index =>$admin){
array_push($liste_diff,$admin[0]);
}
}
$liste_diff = array_unique($liste_diff);
var_export($liste_diff);
Voici le retour de var_export :array ( 0 => '[email protected]', 1 => '[email protected]', 2 => '[email protected]', 3 => '[email protected]', 4 => '[email protected]', )
Voici le code de phpmailer qui envoi les mails :
$mail = new PHPMailer(true);
try {
foreach($liste_diff as $diff){
//Server settings
$mail->SMTPDebug = 0; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = $dmail['smtp']; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $dmail['mail']; //SMTP username
$mail->Password = $dmail['pass']; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = $dmail['port']; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->CharSet = "UTF-8";
//Recipients
$mail->setFrom($dmail['mail']);
$mail->addAddress($diff); //Add a recipient
//$mail->addAddress('[email protected]'); //Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
// $mail->addBCC($diff);
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $objet;
$mail->Body = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if($mail->send()){
$mail_ok = true;
}
}
merci pour vos retous