php电子邮件提醒脚本问题

时间:2016-10-20 08:37:37

标签: php email reminders

我正在构建一个脚本,用于检查将在7天内过期的日期,并向管理所有者发送电子邮件提醒,并更新数据库中的日期通知列,我已经有了它的工作,它会更新日期通知栏并发送电子邮件,但它只列出电子邮件中的一个日期,因为我应该有两个日期,因为我设置两个日期在7天内到期,任何人都可以帮助,下面是我的编码

<?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\r\n";    
    $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']} \n\n";

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

        $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);

       ?>

提前谢谢

1 个答案:

答案 0 :(得分:0)

您的脚本工作正常,它应该在每个到期日期(根据您的代码)发送一封电子邮件,如果域所有者拥有两个域,他将收到两封单独的电子邮件,其中每个都与某个域相关。

如果您想在同一封电子邮件中发送这两个域,请使用以下脚本:

 <?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\r\n";    
        $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']} \n\n";

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

}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']} \n\n";

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


            }

            }

            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);

           ?>