Send email to all registered

0

How can I send an email to each registered user in the database?

In this way I can only send to a user, in this case an email is sent to the last user of bd.

<?php
    header("Content-Type: text/html; charset=UTF-8");

    $sql = "SELECT * FROM 'user'"; 
    $query = $mysqli->query( $sql ); 
    if( $query->num_rows > 0 ) {

         while($row = $query->fetch_assoc()) {
             $email[]=$row['email'];
             $u=$row['username'];    
         }

        $to = "$email";
        $subject = "Assunto";
        $txt = "texto exemplo";

        $headers = "From: suporte@localhost" . "\r\n" .
        "CC: suporte@localhost";

        mail($to,$subject,$txt,$headers);

    } else {

    }
?>
    
asked by anonymous 27.03.2016 / 19:27

1 answer

1

Try something like the example below. His problem was that the mail function was out of the loop, so it would only be executed once. Within the while it will send an email for each record.

header("Content-Type: text/html; charset=UTF-8");

$sql = "SELECT * FROM 'user'"; 
$query = $mysqli->query( $sql ); 

if( $query->num_rows > 0 ) {

    $headers = "From: suporte@localhost"."\r\n"."CC: suporte@localhost";

    while($row = $query->fetch_assoc()) {


        mail($row['email'], "Assunto", "texto exemplo", $headers);
    }
}

I recommend reading and using PHPMailer

    
28.03.2016 / 14:05