continue;
     }
     if ($json_a[$key]['errors'] >= 7) {
         echo "\tToo many errors. Adding domain to removal queue.\n";
         $removal_queue[] = $key;
     }
     if (strpos($errortexts, 'Domain has expired certificate in chain') === false) {
         continue;
     }
 }
 $raw_chain = get_raw_chain($domain);
 $counter = 0;
 foreach ($raw_chain['chain'] as $chain_key => $chain_value) {
     $counter += 1;
     $cert_exp_date = cert_expiry_date($chain_value);
     $cert_cn = cert_cn($chain_value);
     $cert_expiry = cert_expiry($chain_value);
     echo "\tCert Chain #" . $counter . ". Expiry Date: " . date("Y-m-d H:i:s T", $cert_exp_date) . ". Common Name: " . $cert_cn . "\n";
     cert_expiry_emails($domain, $email, $cert_expiry, $chain_value);
 }
 $file = file_get_contents($check_file);
 if ($file === FALSE) {
     echo "\tCan't open database.\n";
     continue;
 }
 $json_a = json_decode($file, true);
 if ($json_a === null && json_last_error() !== JSON_ERROR_NONE) {
     echo "\tCan't read database\n";
     continue;
 }
 if ($json_a[$key]['errors'] != 0) {
function send_expires_in_email($days, $domain, $email, $raw_cert)
{
    global $current_domain;
    global $check_file;
    $domain = trim($domain);
    echo "\t\tDomain " . $domain . " expires in " . $days . " days.\n";
    $file = file_get_contents($check_file);
    if ($file === FALSE) {
        echo "\t\tCan't open database.\n";
        return false;
    }
    $json_a = json_decode($file, true);
    if ($json_a === null && json_last_error() !== JSON_ERROR_NONE) {
        echo "\t\tCan't read database.\n";
        return false;
    }
    foreach ($json_a as $key => $value) {
        if ($value["domain"] == $domain && $value["email"] == $email) {
            $id = $key;
            $cert_cn = cert_cn($raw_cert);
            $cert_subject = cert_subject($raw_cert);
            $cert_serial = cert_serial($raw_cert);
            $cert_expiry_date = cert_expiry_date($raw_cert);
            $cert_validfrom_date = cert_valid_from($raw_cert);
            $now = time();
            $datefromdiff = $now - $cert_validfrom_date;
            $datetodiff = $cert_expiry_date - $now;
            $cert_valid_days_ago = floor($datefromdiff / (60 * 60 * 24));
            $cert_valid_days_ahead = floor($datetodiff / (60 * 60 * 24));
            $unsublink = "https://" . $current_domain . "/unsubscribe.php?id=" . $id;
            $to = $email;
            $subject = "A certificate for " . htmlspecialchars($domain) . " expires in " . htmlspecialchars($days) . " days";
            $message = "Hello,\r\n\r\nYou have a subscription to monitor the certificate of " . htmlspecialchars($domain) . " with the the Certificate Expiry Monitor. This is a service which monitors an SSL certificate on a website, and notifies you when it is about to expire. This extra notification helps you remember to renew your certificate on time.\r\n\r\nWe've noticed that the following domain has a certificate in it's chain that will expire in " . htmlspecialchars($days) . " days:\r\n\r\nDomain: " . htmlspecialchars($domain) . "\r\nCertificate Common Name: " . htmlspecialchars($cert_cn) . "\r\nCertificate Subject: " . htmlspecialchars($cert_subject) . "\r\nCertificate Serial: " . htmlspecialchars($cert_serial) . "\r\nCertificate Valid From: " . htmlspecialchars(date("Y-m-d  H:i:s T", $cert_validfrom_date)) . " (" . $cert_valid_days_ago . " days ago)\r\nCertificate Valid Until: " . htmlspecialchars(date("Y-m-d  H:i:s T", $cert_expiry_date)) . " (" . $cert_valid_days_ahead . " days left)\r\n\r\nYou should renew and replace your certificate before it expires. If you haven't set up the certificate yourself, please forward this email to the person/company that did this for you.\r\n\r\nNot replacing your certificate before the expiry date will result in a non-functional website with errors.\r\n\r\nTo unsubscribe from notifications for this domain please click or copy and paste the below link in your browser:\r\n\r\n" . $unsublink . "\r\n\r\n\r\n Have a nice day,\r\nThe Certificate Expiry Monitor Service.\r\nhttps://" . $current_domain . "";
            $message = wordwrap($message, 70, "\r\n");
            $headers = 'From: noreply@' . $current_domain . "\r\n" . 'Reply-To: noreply@' . $current_domain . "\r\n" . 'Return-Path: noreply@' . $current_domain . "\r\n" . 'X-Visitor-IP: ' . $visitor_ip . "\r\n" . 'X-Coffee: Black' . "\r\n" . 'List-Unsubscribe: <https://' . $current_domain . "/unsubscribe.php?id=" . $id . ">" . "\r\n" . 'X-Mailer: PHP/4.1.1';
            if (mail($to, $subject, $message, $headers) === true) {
                echo "\t\tEmail sent to {$to}.\n";
                return true;
            } else {
                echo "\t\tCan't send email.\n";
                return false;
            }
        }
    }
}