diff --git a/src/api/controllers/BaseController.php b/src/api/controllers/BaseController.php index eb4ede8..1271d60 100644 --- a/src/api/controllers/BaseController.php +++ b/src/api/controllers/BaseController.php @@ -7,11 +7,13 @@ abstract class BaseController { protected $logger; protected $dbLogger; protected $strings; + protected $mailer; public function __construct(ContainerInterface $container) { $this->apiJson = new ApiJson(); $this->logger = $container->get('logger'); $this->dbLogger = new DbLogger(); + $this->mailer = new Mailer('en'); $this->loadStrings('en'); // Default to English } @@ -73,10 +75,13 @@ abstract class BaseController { private function loadStrings($lang) { $json = '{}'; + if (!$lang) { $lang = 'en'; } + $this->mailer = new Mailer($lang); + try { $json = file_get_contents(__DIR__ . '/../../strings/' . $lang . '_api.json'); diff --git a/src/api/helpers/Mailer.php b/src/api/helpers/Mailer.php new file mode 100644 index 0000000..0872ab9 --- /dev/null +++ b/src/api/helpers/Mailer.php @@ -0,0 +1,143 @@ +loadStrings($lang); + } + + /** + * Send email to one or more users in the provided list. + * @param $users List of users to email (must have at least one). + * @param $data Object containing template type and replacements for template. + */ + public function sendMail($users, $data) { + $this->initMail(); + + if (count($users) < 1) { + return $this->strings->mail_error; + } + + foreach($users as $user) { + $this->mail->addAddress($user->email); + } + + $this->mail->Subject = $this->strings->mail_subject; + $this->mail->msgHTML($this->parseTemplate($data)); + + if (!$this->mail->send()) { + return $this->strings->mail_error; + } else { + return $this->strings->mail_sent; + } + } + + private function parseTemplate($data) { + $template = $this->getTemplate($data->type); + + str_replace('%username%', $data->username, $template); + str_replace('%boardName%', $data->boardName, $template); + str_replace('%taskName%', $data->taskName, $template); + str_replace('%comment%', $data->comment, $template); + str_replace('%taskDescription%', $data->taskDescription, $template); + str_replace('%taskDueDate%', $data->taskDueDate, $template); + str_replace('%taskAssignees%', $data->taskAssignees, $template); + str_replace('%taskCategories%', $data->taskCategories, $template); + str_replace('%taskPoints%', $data->taskPoints, $template); + str_replace('%taskColumnName%', $data->taskColumnName, $template); + str_replace('%taskPosition%', $data->taskPosition, $template); + str_replace('%hostUrl%', $data->hostUrl, $template); + str_replace('%boardId%', $data->boardId, $template); + + return $template; + } + + private function getTemplate($type) { + $template = ''; + + switch($type) { + case 'editBoard': + $template = $this->strings->mail_template_editBoard; + break; + + case 'editComment': + $template = $this->strings->mail_template_editComment; + break; + + case 'editTask': + $template = $this->strings->mail_template_editTask; + break; + + case 'newBoard': + $template = $this->strings->mail_template_newBoard; + break; + + case 'newComment': + $template = $this->strings->mail_template_newComment; + break; + + case 'newTask': + $template = $this->strings->mail_template_newTask; + } + + $template .= $this->strings->mail_template_openBoardLink; + + return $template; + } + + private function initMail() { + $this->mail = new PHPMailer(); + $this->mail->isSendmail(); + + $this->mail->setFrom($this->FROM_EMAIL, $this->FROM_NAME); + + if (!$this->USE_SENDMAIL) { + $this->mail->isSMTP(); + + $this->mail->Host = $this->SMTP_HOST; + $this->mail->Port = $this->SMTP_PORT; + $this->mail->Username = $this->SMTP_USER; + $this->mail->Password = $this->SMTP_PASS; + + $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; + $this->mail->SMTPAuth = true; + } + } + + private function loadStrings($lang) { + $json = '{}'; + + if (!$lang) { + $lang = 'en'; + } + + try { + $json = + file_get_contents(__DIR__ . '/../../strings/' . $lang . '_api.json'); + } catch (Exception $ex) { + $ex->getMessage(); + + $json = file_get_contents(__DIR__ . '/../../json/' . $lang . '_api.json'); + } + + $this->strings = json_decode($json); + } +} diff --git a/src/json/en_api.json b/src/json/en_api.json index 7590081..db73536 100644 --- a/src/json/en_api.json +++ b/src/json/en_api.json @@ -61,5 +61,17 @@ "api_userRemoveError": "Error removing user. No user found for ID ", "api_userRemoved": "User removed ", "api_usernameExists": "Username already exists. Change the username and try again.", - "api_userBadPword": "Error updating user. Incorrect current password." + "api_userBadPword": "Error updating user. Incorrect current password.", + + "mail_sent": "Email sent.", + "mail_error": "Email failed to send.", + "mail_subject": "TaskBoard Notification", + + "mail_template_openBoardLink": "

Open TaskBoard", + "mail_template_editBoard": "%username% edited board %boardName%", + "mail_template_editComment": "%username% edited comment in board %boardName%.
Task: %taskName%
Comment: %comment%", + "mail_template_editTask": "%username% edited task in board %boardName%.
Task: %taskName%
Description: %taskDescription%
Due Date: %taskDueDate%
Asignees: %taskAssignees%
Categories: %taskCategories%
Points: %taskPoints%
Column: %taskColumnName%
Position: %taskPosition%", + "mail_template_newBoard": "%username% added board %boardName%", + "mail_template_newComment": "%username% added a comment in board %boardName%.
Task: %taskName%
Comment: %comment%", + "mail_template_newTask": "%username% added a task in board %boardName%.
Task: %taskName%
Description: %taskDescription%
Due Date: %taskDueDate%
Asignees: %taskAssignees%
Categories: %taskCategories%
Points: %taskPoints%
Column: %taskColumnName%
Position: %taskPosition%" } diff --git a/src/json/es_api.json b/src/json/es_api.json index aa684db..d16ab3f 100644 --- a/src/json/es_api.json +++ b/src/json/es_api.json @@ -61,5 +61,17 @@ "api_userRemoveError": "Error al eliminar usuario. No se encontró ningún usuario para ID ", "api_userRemoved": "Usuario eliminado ", "api_usernameExists": "El nombre de usuario ya existe. Cambie el nombre de usuario e intente nuevamente.", - "api_userBadPword": "Error al actualizar el usuario. Contraseña actual incorrecta." + "api_userBadPword": "Error al actualizar el usuario. Contraseña actual incorrecta.", + + "mail_sent": "Correo electrónico enviado", +  "mail_error": "Error al enviar el correo electrónico", +  "mail_subject": "Notificación del tablero de tareas", + +  "mail_template_openBoardLink": "

Abrir TaskBoard", +  "mail_template_editBoard": "%username% tablero editado %boardName%", +  "mail_template_editComment": "%username% comentario editado en el tablero %boardName%.
Tarea: %taskName%
Comentario: %comment%", +  "mail_template_editTask": "%username% tarea editada en el tablero %boardName%.
Tarea: %taskName%
Descripción: %taskDescription%
Fecha de vencimiento: %taskDueDate%
Asignees: %taskAssignees%
Categorías: %taskCategories%
Puntos: %taskPoints%
Columna: %taskColumnName%
Posición: %taskPosition% ", +  "mail_template_newBoard": "%username% placa agregada %boardName%", +  "mail_template_newComment": "%username% agregó un comentario en el tablero %boardName%.
Tarea: %taskName%
Comentario: %comment%", +  "mail_template_newTask": "%username% agregó una tarea en el tablero %boardName%.
Tarea: %taskName%
Descripción: %taskDescription%
Fecha de vencimiento: %taskDueDate%
Asignees: %taskAssignees%
Categorías: %taskCategories%
Puntos: %taskPoints%
Columna: %taskColumnName%
Posición: %taskPosition% " } diff --git a/src/json/fr_api.json b/src/json/fr_api.json index 3e290e6..da83de0 100644 --- a/src/json/fr_api.json +++ b/src/json/fr_api.json @@ -61,5 +61,17 @@ "api_userRemoveError": "Erreur lors de la suppression de l'utilisateur. Aucun utilisateur trouvé pour l'ID ", "api_userRemoved": "Utilisateur supprimé ", "api_usernameExists": "Le nom d'utilisateur existe déjà. Modifiez le nom d'utilisateur et réessayez.", - "api_userBadPword": "Erreur lors de la mise à jour de l'utilisateur. Mot de passe actuel incorrect." + "api_userBadPword": "Erreur lors de la mise à jour de l'utilisateur. Mot de passe actuel incorrect.", + + "mail_sent": "E-mail envoyé.", +  "mail_error": "E-mail non envoyé.", +  "mail_subject": "Notification TaskBoard", + +  "mail_template_openBoardLink": "

Ouvrir TaskBoard", +  "mail_template_editBoard": "%username% a modifié la carte %boardName%", +  "mail_template_editComment": "%username% commentaire modifié dans le tableau %boardName%.
Tâche: %taskName%
Commentaire: %comment%", +  "mail_template_editTask": "%username% tâche modifiée dans la carte %boardName%.
Tâche: %taskName%
Description: %taskDescription%
Date d'échéance: %taskDueDate%
Destinataires: %taskAssignees%
Catégories: %taskCategories%
Points: %taskPoints%
Colonne: %taskColumnName%
Position: %taskPosition%", +  "mail_template_newBoard": "%username% a ajouté la carte %boardName%", +  "mail_template_newComment": "%username% a ajouté un commentaire dans le tableau %boardName%.
Tâche: %taskName%
Commentaire: %comment%", +  "mail_template_newTask": "%username% a ajouté une tâche dans la carte %boardName%.
Tâche: %taskName%
Description: %taskDescription%
Date d'échéance: %taskDueDate%
Destinataires: %taskAssignees%
Catégories: %taskCategories%
Points: %taskPoints%
Colonne: %taskColumnName%
Position: %taskPosition%" }