diff($today)->days <= 14) { $allMessagesToSend[$currentUserId][$message['msghash']] = [ 'thedate' => $message['thedate'], 'subject' => $message['subject'], 'message' => $message['message'], 'projectId' => $message['projectId'], ]; } // DONE here : here we need a message id to allow deleting messages of the queue when they are sent // and here we need to group the messages in an array to know which messages are grouped to group-delete them // Discard all messages $allMessagesToDelete[$currentUserId][] = $message['msghash']; } foreach ($allMessagesToSend as $currentUserId => $messageToSendToUser) { $theuser = $this->userRepo->getUser($currentUserId); if ($theuser === false) { continue; } $recipient = $theuser['username']; // DONE : Deal with users parameters to allow them define a maximum (and minimum ?) frequency to receive mails $lastMessageDate = strtotime($this->settingsRepo->getSetting('usersettings.'.$theuser['id'].'.lastMessageDate')); $nowDate = time(); // echo for DEBUG PURPOSE // debug_print("Last message to " . $recipient . " was on " . date('Y-m-d H:i:s', $lastMessageDate)); $timeSince = abs($nowDate - $lastMessageDate); // Get company message frequency default $messageFrequency = $this->settingsRepo->getSetting('companysettings.messageFrequency'); // Check if user has frequency set if (empty($messageFrequency)) { $messageFrequency = $this->settingsRepo->getSetting('usersettings.'.$theuser['id'].'.messageFrequency'); } // Last security to avoid flooding people. if (empty($messageFrequency)) { $messageFrequency = 900; } // echo for DEBUG PURPOSE // debug_print("The message frequency for " . $recipient . " : " . $messageFrequency); if ($timeSince < $messageFrequency) { // echo for DEBUG PURPOSE // debug_print("Elapsed time not enough for " . $recipient . " : skipping till " . date("Y-m-d H:i:s", $lastMessageDate + $messageFrequency)); continue; } // TODO here : set up a true templating system to format the messages $formattedHTML = $this->doFormatMail($messageToSendToUser); // DONE Tranlastion needed somewhere ? // DONE : Send the message with PHPMailer here $this->mailer->setContext('latest_updates'); if (count($messageToSendToUser) == 1) { reset($messageToSendToUser); $this->mailer->setSubject(current($messageToSendToUser)['subject']); } else { $this->mailer->setSubject($this->language->__('email_notifications.latest_updates_subject')); } $this->mailer->setHtml($formattedHTML); $to = [$recipient]; $this->mailer->sendMail($to, 'Leantime System'); // Delete the corresponding messages from the queue when the mail is sent // TODO here : only delete these if the send was successful // echo for DEBUG PURPOSE // debug_print("Messages send (about to delete) :"); $this->queue->deleteMessageInQueue($allMessagesToDelete[$currentUserId]); // Store the last time a mail was sent to $recipient email $thedate = date('Y-m-d H:i:s'); $this->settingsRepo->saveSetting('usersettings.'.$theuser['id'].'.lastMessageDate', $thedate); } } // Fake template to be replaced by something better // TODO : Rework email templating system private function doFormatMail($messageToSendToUser): string { $outputHTML = $this->language->__('text.here_are_news')."
\n"; foreach ($messageToSendToUser as $chunk) { $outputHTML .= '
'; $outputHTML .= '
'.$chunk['thedate'].'
'; $outputHTML .= '

'.$chunk['subject'].'

'; $outputHTML .= $chunk['message'].'
'; $outputHTML .= '
'; } return $outputHTML; } }