Commit 70c6ac77 authored by Thomas Bella's avatar Thomas Bella

Add configurable email notification interval

Fixes #18
parent 49ba0f90
Pipeline #529 passed with stage
in 32 seconds
......@@ -12,3 +12,7 @@ if (!defined("EMAIL")) {
if (!defined("EXPORT")) {
define("EXPORT", true);
}
if (!defined("EMAIL_NOTIFICATION_INTERVAL")) {
define("EMAIL_NOTIFICATION_INTERVAL", json_encode([-3, 0, 1, 3, 7, 14, 29]));
}
......@@ -30,6 +30,8 @@ define("EMAIL_PASSWORD", "secret");
define("EMAIL_FROM", EMAIL_USERNAME);
// default recipient. can be one or multible addresses like me@example.com;you@example.com separated with ";"
define("EMAIL_TARGET", "");
// email notification interval until expiration. Default: "-3, 0, 1, 3, 7, 14, 29" (each element describes a day)
define("EMAIL_NOTIFICATION_INTERVAL", json_encode([-3, 0, 1, 3, 7, 14, 29]));
// Enable or disable export of database (default: true)
define("EXPORT", true);
......@@ -214,7 +214,6 @@ foreach ($ressources as $row) {
} else {
$emailList = $globalEmailList;
}
vlog('Found ' . count($emailList) . ' EMail adresses to notify', 2);
// Determine days until certificate expires
$expiryDate = CertificateReader::expiryDate($cert);
......@@ -222,37 +221,25 @@ foreach ($ressources as $row) {
vlog($daysUntilExpire . ' days until expiration', 2);
// Detect notification state
if ($daysUntilExpire < 0 && $row['notification_step'] < 4) {
vlog('Certificate is expired', 2);
$days = -1;
$index = 4;
} else if ($daysUntilExpire <= 3 && $row['notification_step'] < 3) {
vlog('3 days until expiration', 2);
$days = 3;
$index = 3;
} else if ($daysUntilExpire <= 7 && $row['notification_step'] < 2) {
vlog('7 days until expiration', 2);
$days = 7;
$index = 2;
} else if ($daysUntilExpire <= 29 && $row['notification_step'] < 1) {
vlog('29 days until expiration', 2);
$days = 29;
$index = 1;
} else {
// Reset expiry to 0 to reenable notifications
if ($daysUntilExpire > 29) {
vlog('Resetting expiration counter', 2);
$SQL->query("UPDATE log SET notification_step = :notstep WHERE domain_id = :id", [ "notstep" => 0, "id" => $row["id"] ]);
} else {
vlog('Notification already sent', 2);
$notificationInterval = json_decode(EMAIL_NOTIFICATION_INTERVAL, true);
// Sort from low to high
asort($notificationInterval);
$sendNotification = false;
foreach ($notificationInterval as $singleNotificationDay) {
if ($daysUntilExpire <= $singleNotificationDay) {
vlog('Found. ' . $daysUntilExpire . ' days <= ' . $singleNotificationDay . ' days. Checking if notification should be sent', 3);
if ($row['notification_step'] != $singleNotificationDay) {
vlog('Notification wasn\'t sent already', 3);
$sendNotification = true;
$SQL->query("UPDATE log SET notification_step = :notstep WHERE domain_id = :id", [ "notstep" => $singleNotificationDay, "id" => $row["id"] ]);
}
break;
}
$days = false;
}
// Send notifications
if ($days !== false) {
$SQL->query("UPDATE log SET notification_step = :notstep WHERE domain_id = :id", [ "notstep" => $index, "id" => $row["id"] ]);
if ($sendNotification) {
$mail = new PHPMailer;
if (EMAIL_SMTP) {
......@@ -268,8 +255,8 @@ foreach ($ressources as $row) {
}
}
// Set priority to high if expiration date is near
if ($days <= 3) {
// Set priority to high if expiration is near (7 days or less)
if ($daysUntilExpire <= 7) {
$mail->Priority = 1;
$mail->AddCustomHeader('X-MSMail-Priority: High');
$mail->AddCustomHeader('Importance: High');
......@@ -280,9 +267,10 @@ foreach ($ressources as $row) {
// Use port function to print protocol in addition to port
$protocolName = $ports[$row['port']]['name'];
$mail->Body = 'Your certificate for ' . $row['domain'] . ':' . $row['port'] . ' (' . $protocolName . ') will expire in about ' . $days . ' days (' . date("d.m.Y H:i", $expiryDate) . ').' . "\n" .
$mail->Body = 'Your certificate for ' . $row['domain'] . ':' . $row['port'] . ' (' . $protocolName . ') will expire in about ' . $daysUntilExpire . ' days (' . date("d.m.Y H:i", $expiryDate) . ').' . "\n" .
'Please renew your certificate.' . "\n\n" . 'Certificate Monitor on ' . BASEURL;
vlog('Found ' . count($emailList) . ' EMail adresses to notify', 2);
foreach ($emailList as $singleEmail) {
vlog('Sending EMail to ' . $singleEmail, 2);
$mail->addAddress($singleEmail);
......@@ -291,7 +279,7 @@ foreach ($ressources as $row) {
}
$mail->clearAddresses();
}
} // $days not false
} // $sendNotification
} // EMAIL enabled
unset($certs, $cert);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment