system/library/notification/email_notification.php
Вот содержимое файла system/library/notification/email_notification.php
:
<?php
// Email Notification Library for Task Manager - Handles sending email notifications
class EmailNotification {
private $from_name;
private $from_email;
private $subject;
private $message;
private $to_email;
public function __construct($from_name, $from_email, $subject, $message, $to_email) {
$this->from_name = $from_name;
$this->from_email = $from_email;
$this->subject = $subject;
$this->message = $message;
$this->to_email = $to_email;
}
// Send email using PHP's mail() function
public function sendEmail() {
// Set email headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
$headers .= "From: " . $this->from_name . " <" . $this->from_email . ">" . "\r\n";
$headers .= "Reply-To: " . $this->from_email . "\r\n";
// Send the email
$mail_sent = mail($this->to_email, $this->subject, $this->message, $headers);
// Check if the email was sent successfully
if ($mail_sent) {
return array('success' => true, 'message' => 'Email sent successfully');
} else {
return array('success' => false, 'message' => 'Failed to send email');
}
}
// Method to send task-related notifications
public function sendTaskNotification($task_name, $user_email) {
// Build the email message for task notification
$this->subject = "Task Notification: " . $task_name;
$this->message = "<html><body>";
$this->message .= "<h3>You have a new task assigned to you: " . $task_name . "</h3>";
$this->message .= "<p>Please check your task manager for more details.</p>";
$this->message .= "</body></html>";
$this->to_email = $user_email;
return $this->sendEmail();
}
// Method to send task completion notifications
public function sendTaskCompletionNotification($task_name, $user_email) {
// Build the email message for task completion
$this->subject = "Task Completed: " . $task_name;
$this->message = "<html><body>";
$this->message .= "<h3>Task Completed: " . $task_name . "</h3>";
$this->message .= "<p>The task has been completed successfully. You can now review the task details in your task manager.</p>";
$this->message .= "</body></html>";
$this->to_email = $user_email;
return $this->sendEmail();
}
// Method to send reminder notifications for tasks
public function sendTaskReminderNotification($task_name, $user_email, $reminder_time) {
// Build the email message for task reminder
$this->subject = "Reminder: " . $task_name;
$this->message = "<html><body>";
$this->message .= "<h3>Reminder: " . $task_name . "</h3>";
$this->message .= "<p>This is a reminder for your task. The task is due at " . $reminder_time . ". Please complete it on time.</p>";
$this->message .= "</body></html>";
$this->to_email = $user_email;
return $this->sendEmail();
}
// Method to send task overdue notifications
public function sendTaskOverdueNotification($task_name, $user_email) {
// Build the email message for overdue task
$this->subject = "Task Overdue: " . $task_name;
$this->message = "<html><body>";
$this->message .= "<h3>Your task is overdue: " . $task_name . "</h3>";
$this->message .= "<p>Please take immediate action to complete the task as soon as possible.</p>";
$this->message .= "</body></html>";
$this->to_email = $user_email;
return $this->sendEmail();
}
// Method to send task status change notifications
public function sendTaskStatusChangeNotification($task_name, $user_email, $new_status) {
// Build the email message for task status change
$this->subject = "Task Status Changed: " . $task_name;
$this->message = "<html><body>";
$this->message .= "<h3>Task Status Updated: " . $task_name . "</h3>";
$this->message .= "<p>The status of your task has been updated to: " . $new_status . ". Please check your task manager for more details.</p>";
$this->message .= "</body></html>";
$this->to_email = $user_email;
return $this->sendEmail();
}
}
?>
Объяснение кода:
-
Класс
EmailNotification
:-
Этот класс используется для отправки различных уведомлений по электронной почте, таких как уведомления о задаче, напоминания, уведомления о завершении задачи и т. д.
-
В конструкторе класса передаются параметры для отправки письма, такие как имя отправителя, email отправителя, тема письма, тело письма и email получателя.
-
-
Методы класса:
-
sendEmail()
: Метод для отправки email-уведомлений с помощью стандартной функцииmail()
. Он также задает необходимые заголовки для HTML-письма. -
sendTaskNotification($task_name, $user_email)
: Отправляет уведомление пользователю о назначенной задаче. -
sendTaskCompletionNotification($task_name, $user_email)
: Отправляет уведомление о завершении задачи. -
sendTaskReminderNotification($task_name, $user_email, $reminder_time)
: Отправляет напоминание пользователю о задаче. -
sendTaskOverdueNotification($task_name, $user_email)
: Отправляет уведомление о просроченной задаче. -
sendTaskStatusChangeNotification($task_name, $user_email, $new_status)
: Отправляет уведомление о изменении статуса задачи.
-
Зависимости:
-
PHP mail(): Используется для отправки email-сообщений. Убедитесь, что сервер настроен для отправки почты (например, через
sendmail
или SMTP-сервер).
Этот файл будет использоваться в модуле для отправки уведомлений пользователям, связанных с задачами, например, уведомления о новых задачах, изменении статуса задач и т. д.