how to send several emails with checkbox and phpmailer

0

I need to send an email to several recipients from a database I do it with a while and if it shows me the emails that I have but when sending it, it only sends it to the first email that I have in the list, this is my code. .

  $query="SELECT * FROM preescolar";
                $result=mysqli_query($db_link, $query);
        while ($row=mysqli_fetch_array($result)){
                echo $row['correo'];


                if (isset($_POST['send'])){
                    include("masivo_config.php");//Mando a llamar la funcion que se encarga de enviar el correo electronico


                    /*Configuracion de variables para enviar el correo*/
                    $correo=$row['correo'];



//////TE REFIERES A ESTE:
function sendemail( $contador, $titulo, $cuerpo, $archivo){

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '587';
$mail->Username = '[email protected]';
$mail->Password = 'tecnologiatula';
$mail->setFrom('');
$mail->FromName = "COORDINACION DE TECNOLOGIA EDUCATIVA";

$mail->addAddress($correo);
$mail->addBCC($contador);
$mail->Subject = $titulo;
$mail->Body = ($cuerpo);
$mail->IsHTML(true); 
    if($archivo["size"] > 0){
    $mail->addAttachment($archivo["tmp_name"], $archivo["name"]);

     }

if(!$mail->send()) {

echo 'Error de correo:'.$mail->ErrorInfo;       
    } else {

        header('location:mensaje_enviado.php');
    }

}
    
asked by Monica Zuniga 13.08.2018 в 19:26
source

1 answer

0

Here I leave the correction of the $ mail please check the code well because there is data that I do not know where you are taking it.

Note: if you send an error on the screen please put it.

$query="SELECT * FROM preescolar";
$result=mysqli_query($db_link, $query);
while ($row=mysqli_fetch_array($result)){
 //echo $row['correo'];

 if (isset($_POST['send'])){
     /*Configuracion de variables para enviar el correo*/
     $correo = $row['correo'];
     $contador = 'correo del contador';
     $titulo =  'Asunto del correo';
     $cuerpo =  'Cuerpo del correo '; 

     sendemail($correo,$contador,$titulo, $cuerpo);

    }
} 

header('location:mensaje_enviado.php');

//////TE REFIERES A ESTE:
function sendemail($correo, $contador, $titulo, $cuerpo, $archivo = ''){

    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = '587';
    $mail->Username = '[email protected]';
    $mail->Password = 'tecnologiatula';
    $mail->setFrom('');
    $mail->FromName = "COORDINACION DE TECNOLOGIA EDUCATIVA";

    $mail->addAddress($correo);
    $mail->addBCC($contador);
    $mail->Subject = $titulo;
    $mail->Body = ($cuerpo);
    $mail->IsHTML(true); 
    if($archivo["size"] > 0){
        $mail->addAttachment($archivo["tmp_name"], $archivo["name"]);
    }
    if(!$mail->send()) {
        echo 'Error de correo:'.$mail->ErrorInfo;       
    } 
}
    
answered by 14.08.2018 в 19:59