Initial email setup in place

This commit is contained in:
Matthew Ross 2020-05-13 11:37:17 -04:00
parent 3eb38905b9
commit c58bea87cf
5 changed files with 187 additions and 3 deletions

View File

@ -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');

143
src/api/helpers/Mailer.php Normal file
View File

@ -0,0 +1,143 @@
<?php
use PHPMailer\PHPMailer\PHPMailer;
class Mailer {
// Edit these values for your email setup.
const USE_SENDMAIL = true; // If false, uses SMTP settings below
const FROM_EMAIL = 'taskboard@' . $_SERVER['HTTP_HOST'];
const FROM_NAME = 'TaskBoard';
// SMTP Settings - Not used if USE_SENDMAIL is true
const SMTP_HOST = 'smtp.gmail.com';
const SMTP_PORT = 587;
const SMTP_USER = 'you@gmail.com';
const SMTP_PASS = 'yourPassword';
// End SMTP Settings
// DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING
private $strings;
private $mail;
public function __construct(string $lang)
{
$this->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);
}
}

View File

@ -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": "<br><br><a href=\"%hostUrl%/boards/%boardId%\">Open TaskBoard</a>",
"mail_template_editBoard": "%username% edited board %boardName%",
"mail_template_editComment": "%username% edited comment in board %boardName%.<br>Task: %taskName%<br>Comment: %comment%",
"mail_template_editTask": "%username% edited task in board %boardName%.<br>Task: %taskName%<br>Description: %taskDescription%<br>Due Date: %taskDueDate%<br>Asignees: %taskAssignees%<br>Categories: %taskCategories%<br>Points: %taskPoints%<br>Column: %taskColumnName%<br>Position: %taskPosition%",
"mail_template_newBoard": "%username% added board %boardName%",
"mail_template_newComment": "%username% added a comment in board %boardName%.<br>Task: %taskName%<br>Comment: %comment%",
"mail_template_newTask": "%username% added a task in board %boardName%.<br>Task: %taskName%<br>Description: %taskDescription%<br>Due Date: %taskDueDate%<br>Asignees: %taskAssignees%<br>Categories: %taskCategories%<br>Points: %taskPoints%<br>Column: %taskColumnName%<br>Position: %taskPosition%"
}

View File

@ -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": "<br><br><a href=\"%hostUrl%/boards/%boardId%\">Abrir TaskBoard</a>",
  "mail_template_editBoard": "%username% tablero editado %boardName%",
  "mail_template_editComment": "%username% comentario editado en el tablero %boardName%.<br>Tarea: %taskName%<br>Comentario: %comment%",
  "mail_template_editTask": "%username% tarea editada en el tablero %boardName%.<br>Tarea: %taskName%<br>Descripción: %taskDescription%<br>Fecha de vencimiento: %taskDueDate%<br>Asignees: %taskAssignees%<br>Categorías: %taskCategories%<br>Puntos: %taskPoints%<br>Columna: %taskColumnName%<br>Posición: %taskPosition% ",
  "mail_template_newBoard": "%username% placa agregada %boardName%",
  "mail_template_newComment": "%username% agregó un comentario en el tablero %boardName%.<br>Tarea: %taskName%<br>Comentario: %comment%",
  "mail_template_newTask": "%username% agregó una tarea en el tablero %boardName%.<br>Tarea: %taskName%<br>Descripción: %taskDescription%<br>Fecha de vencimiento: %taskDueDate%<br>Asignees: %taskAssignees%<br>Categorías: %taskCategories%<br>Puntos: %taskPoints%<br>Columna: %taskColumnName%<br>Posición: %taskPosition% "
}

View File

@ -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": "<br><br><a href=\"%hostUrl%/boards/%boardId%\">Ouvrir TaskBoard</a>",
  "mail_template_editBoard": "%username% a modifié la carte %boardName%",
  "mail_template_editComment": "%username% commentaire modifié dans le tableau %boardName%.<br>Tâche: %taskName%<br>Commentaire: %comment%",
  "mail_template_editTask": "%username% tâche modifiée dans la carte %boardName%.<br>Tâche: %taskName%<br>Description: %taskDescription%<br>Date d'échéance: %taskDueDate%<br>Destinataires: %taskAssignees%<br>Catégories: %taskCategories%<br>Points: %taskPoints%<br>Colonne: %taskColumnName%<br>Position: %taskPosition%",
  "mail_template_newBoard": "%username% a ajouté la carte %boardName%",
  "mail_template_newComment": "%username% a ajouté un commentaire dans le tableau %boardName%.<br>Tâche: %taskName%<br>Commentaire: %comment%",
  "mail_template_newTask": "%username% a ajouté une tâche dans la carte %boardName%.<br>Tâche: %taskName%<br>Description: %taskDescription%<br>Date d'échéance: %taskDueDate%<br>Destinataires: %taskAssignees%<br>Catégories: %taskCategories%<br>Points: %taskPoints%<br>Colonne: %taskColumnName%<br>Position: %taskPosition%"
}