How to send e-mail to multiple recipients from database query (PHP)

前端 未结 7 2472
南方客
南方客 2020-12-29 16:41

I\'m trying to send an e-mail to multiple e-mail address in my database. Here is my current code. It is only working when I specify a single e-mail address, however, I need

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 16:59

    I learned to this from one of the examples in the book I am learning PHP from, Head First PHP & MySQL.

    What you can do is send each e-mail individually, but simultaneously, through a while loop. With a mysqli_query selecting the e-mails, make a while loop that goes through my

    while ($row_data = mysqli_fetch_array($your_query_in_a_variable)) {
    
        // Then you will set your variables for the e-mail using the data 
        // from the array.
    
        $from = 'you@yoursite.com';
        $to = $row_data['email']; // The column where your e-mail was stored.
        $subject = 'Open Me!';
        $msg = 'Hello world!';
        mail($to, $msg, $from);
    }
    

    I hope that is clear. You basically just call the rows you want in your query, then use mysqli_fetch_array, which goes through each row every time it is called (someone correct me, if wrong) and will exit when there are no more rows left that were called.

提交回复
热议问题