Send email to all registered

Asked

Viewed 52 times

0

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

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

<?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 {

    }
?>
  • How do you want to do? an email for each person or an email for everyone?

  • an email to each @rray person

  • 1

    Plays the mail() inside the while then.

  • true @rray :)

  • Oops, just a few comments you should pay attention to. How many emails are there? Where is the application hosting will support sending? Aren’t there trigger limits per hour? If you don’t see these details you can simply block sending mail from the server. Among others, how to put the domain in black list and so on...

1 answer

1


Try something like the example below. Your problem was that the mail function was outside the loop, so it would only be excommunicated once. Inside the while she 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 the Phpmailer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.