php电子邮件提醒脚本问题

I am building a script that checks for dates that will expire within 7 days and sends out a email reminder to the admin owner and also update a date notified column in the database, I have got it sort of working, it updates the date notified column and sends the email out but it only lists one date in the email where as there should be two as I set two dates to expire within 7 days, can anyone help please, below is the coding I have

<?php

$db = mysqli_connect("localhost" , "", "") or die("Check connection      parameters!"); 
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)  
mysqli_select_db($db,"") or die(mysqli_error($db));

if (mysqli_connect_error()) {
die ('Failed to connect to MySQL');
} else {
/*SUCCESS MSG*/
echo '';
}

$sqlCommand = "SELECT 
    u.id
    , domain_name_owner
    , url
    , DATE_FORMAT(domain_expiry_date, '%e %M %Y') as domain_expiration_date
    , domain_owner_email
    FROM websites u
    WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
    ";

    $query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

    //fetch the data from the database 
    while ($row = mysqli_fetch_array($query)) {

$arr_ids[] = $row['id'];

    $email = '';
    $headers = "From: noreply@domain.co.uk
";    
    $subject = "Domain Name Expiry Date(s)";
    $message = '';

    $id = $row['id'];
    $owner = $row['domain_name_owner'];
    $email = $row['domain_owner_email'];
    $message = "Domain Name Owner: {$row['domain_name_owner']} 

";

$message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiration_date']}
";

        $to = $email;
        $sendmail = mail($to, $subject, $message, $headers);
        if ($sendmail) {
            echo nl2br($message);
            echo "<b>Email Successfully Sent</b><br><br>";
        } else { 
            echo "<b>Error in Sending of Email to $to</b><br><br>";
        }

        }

        if (isset($arr_ids)){
        $sql = "UPDATE websites SET date_notified_of_domain_expiry = NOW()             WHERE id IN (";
        $sql .= implode("," , $arr_ids);
        $sql .= ");";
        print $sql;

        }

       //$db->query($sql);
       $db->query($sql) or die(mysqli_error($db));

       // Free the results  
       mysqli_free_result($query);

       //close the connection
       mysqli_close($db);

       ?>

Thank you in advance

Your Script is working fine, it should send one email per expiry date (based on your code), if the domain owner owns two domains, he will receive two separate emails that each one of them is related to a certain domain.

If you want to send the two domains on the same email, use the below script:

 <?php

    $db = mysqli_connect("localhost" , "", "") or die("Check connection      parameters!"); 
    // Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)  
    mysqli_select_db($db,"") or die(mysqli_error($db));

    if (mysqli_connect_error()) {
    die ('Failed to connect to MySQL');
    } else {
    /*SUCCESS MSG*/
    echo '';
    }

    $sqlCommand = "SELECT 
        u.id
        , domain_name_owner
        , url
        , DATE_FORMAT(domain_expiry_date, '%e %M %Y') as domain_expiration_date
        , domain_owner_email
        FROM websites u
        WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
        ";

        $query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

        //fetch the data from the database 
$message = '';
$email = '';
$prev_Email ='';
        while ($row = mysqli_fetch_array($query)) {

    $arr_ids[] = $row['id'];

       $prev_Email = $email;
        $headers = "From: noreply@domain.co.uk
";    
        $subject = "Domain Name Expiry Date(s)";
        $id = $row['id'];
        $owner = $row['domain_name_owner'];
        $email = $row['domain_owner_email'];

        if($prev_Email == ""){
           $prev_Email = $email;
        }


if($email == $prev_email && $email != ""){
$message. = "Domain Name Owner: {$row['domain_name_owner']} 

";

    $message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiration_date']}
";

}else{
 if($prev_Email != ""){
$to = $prev_Email;
            $sendmail = mail($to, $subject, $message, $headers);
            if ($sendmail) {
                echo nl2br($message);
                echo "<b>Email Successfully Sent</b><br><br>";
            } else { 
                echo "<b>Error in Sending of Email to $to</b><br><br>";
            }
$message = "Domain Name Owner: {$row['domain_name_owner']} 

";

    $message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiration_date']}
";
}


            }

            }

            if (isset($arr_ids)){
            $sql = "UPDATE websites SET date_notified_of_domain_expiry = NOW()             WHERE id IN (";
            $sql .= implode("," , $arr_ids);
            $sql .= ");";
            print $sql;

            }

           //$db->query($sql);
           $db->query($sql) or die(mysqli_error($db));

           // Free the results  
           mysqli_free_result($query);

           //close the connection
           mysqli_close($db);

           ?>