简体   繁体   中英

Send email to all email address from MySQL using PHP

I am trying to send email to All users from Mysql Database. For this I have written below Code, but it is sending email to First record only. What's wrong I am doing. I need to send email to all users.

if ($_POST['do'] == 'mail') {


$result = $db->query("SELECT email FROM members WHERE status='Active'");

$input="This is a text message";

       $userdetails = $db->fetch_array($result);

       $emails = implode(",", $userdetails);

        $message = $input;
        $mail = new mail();
        $mail->setFrom($settings['email_support'], $input->pc['name']);
        $mail->addTo($emails);
        $mail->setSubject('subject text!');
        $mail->setBodyText($message);
        $mail->send();
}

It seems your code perfect. It can be possible you are fetching only 1 row. If not, then try this it will help you.

if ($_POST['do'] == 'mail') {
    $result = $db->query("SELECT email FROM members WHERE status='Active'");
    $input = "This is a text message";
    foreach ($result as $row) {
        $message = $input;
        $mail = new mail();
        $mail->setFrom($settings['email_support'], $input->pc['name']);
        $mail->addTo($row['email']);
        $mail->setSubject('subject text!');
        $mail->setBodyText($message);
        $mail->send();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM