Loading src/UnicaenMail/Service/Mail/MailService.php +172 −44 Original line number Diff line number Diff line Loading @@ -11,6 +11,7 @@ use Exception; use RuntimeException; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; use UnicaenMail\Entity\Db\Mail; use Laminas\Mail\Message; Loading @@ -20,25 +21,53 @@ use Laminas\Mime\Part; use Laminas\Mvc\Controller\AbstractActionController; use UnicaenMail\Exception\NotFoundConfigException; class MailService { class MailService { use ProvidesObjectManager; const BODY_TEXT_TEMPLATE = <<<EOS ----------------------------------------------------------------------- Ce mail a été redirigé. Destinataires originaux : To: %s Cc: %s Bcc: %s EOS; const BODY_HTML_TEMPLATE = <<<EOS <p>Ce mail a été redirigé.</p> <p> Destinataires originaux :<br /> To: %s<br /> Cc: %s<br /> Bcc: %s </p> EOS; private ?Mailer $mailer; public function getMailer(): ?Mailer { return $this->mailer; } public function setMailer(?Mailer $mailer): void { $this->mailer = $mailer; } private ?string $entityClass = null; private array $config = []; /** * @param string $entityClass * @return MailService Loading @@ -49,17 +78,23 @@ class MailService { return $this; } public function setConfig(array $config): void { $this->config = $config; } public function createMailEntity() : Object public function createMailEntity(): object { $entity = new $this->entityClass(); return $entity; } /** GESTION DES ENTITES *******************************************************************************************/ public function create(Mail $mail): Mail Loading @@ -69,12 +104,16 @@ class MailService { return $mail; } public function update(Mail $mail): Mail { $this->objectManager->flush($mail); return $mail; } public function delete(Mail $mail): Mail { $this->objectManager->remove($mail); Loading @@ -82,6 +121,8 @@ class MailService { return $mail; } /** REQUETAGE *****************************************************************************************************/ public function createQueryBuilder(): QueryBuilder Loading @@ -90,6 +131,8 @@ class MailService { return $qb; } /** * @param string $champ * @param string $ordre Loading @@ -104,6 +147,8 @@ class MailService { return $result; } public function getMailsByMotClef(string $motClef, string $champ = 'dateEnvoi', string $ordre = 'DESC'): array { // todo ici un fix sal au probleme de MotClef1 === MotClef123 avec MotClef1 inclu dans MotClef123 Loading @@ -116,6 +161,8 @@ class MailService { return $result; } public function getMail(?int $id): ?Mail { $qb = $this->createQueryBuilder() Loading @@ -129,6 +176,8 @@ class MailService { return $result; } public function getRequestedMail(AbstractActionController $controller, string $param = 'mail'): ?Mail { $id = $controller->params()->fromRoute($param); Loading @@ -154,6 +203,8 @@ class MailService { return $value; } public function transform(Mail $mail, ?string $module = null): Email { try { Loading Loading @@ -212,6 +263,8 @@ class MailService { return $message; } public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null, $copie = null): ?Mail { try { Loading Loading @@ -271,6 +324,79 @@ class MailService { return $mail; } public function send(Email $mail, ?string $module = null): void { $doNotSend = $this->fetchValueFromConfig('do_not_send', $module); $redirect = $this->fetchValueFromConfig('redirect', $module); if ($doNotSend) { return; } if ($redirect) { $mail = $this->prepareMessageForRedirection($mail, $module); } try { $this->getMailer()->send($mail); } catch (TransportExceptionInterface $e) { throw new RuntimeException("Échec de l'envoi du message", 0, $e); } } protected function prepareMessageForRedirection(Email $mail, ?string $module = null): Email { // On crée un clone $redirMail = clone($mail); // Redirection effective $redirMail->getHeaders()->remove('To'); // Supprime tous les To $redirMail->getHeaders()->remove('Cc'); // Supprime tous les CC $redirMail->getHeaders()->remove('Bcc'); // Supprime tous les BCC $redirectTo = (array)$this->fetchValueFromConfig('redirect_to', $module); foreach ($redirectTo as $to) { $redirMail->addTo($to); } // Modification du sujet $redirMail->subject($mail->getSubject() . " {REDIR}"); // Modification du corps $to = []; $cc = []; $bcc = []; foreach ($mail->getTo() as $addr) { $to[] = $addr->toString(); } foreach ($mail->getCc() as $addr) { $cc[] = $addr->toString(); } foreach ($mail->getBcc() as $addr) { $bcc[] = $addr->toString(); } $to = implode(", ", $to); $cc = implode(", ", $cc); $bcc = implode(", ", $bcc); if ($textBody = $mail->getTextBody()){ $textBody .= sprintf(self::BODY_TEXT_TEMPLATE, $to, $cc, $bcc); $redirMail->text($textBody); } if ($htmlBody = $mail->getHtmlBody()){ $htmlBody .= sprintf(self::BODY_HTML_TEMPLATE, $to, $cc, $bcc); $redirMail->html($htmlBody); } return $redirMail; } /** TODO : Le reenvoi ne tient pas compte du module ... */ public function reenvoi(Mail $mail): Mail { Loading @@ -285,15 +411,17 @@ class MailService { } /** @return Mail[] */ public function getMailsWithFiltre(array $filtres): array { $qb = $this->createQueryBuilder(); if (isset($filtres['statut']) AND $filtres['statut'] !== '') { if (isset($filtres['statut']) and $filtres['statut'] !== '') { $qb = $qb->andWhere('mail.statusEnvoi = :statut')->setParameter('statut', $filtres['statut']); } if (isset($filtres['date']) AND $filtres['date'] !== '') { if (isset($filtres['date']) and $filtres['date'] !== '') { try { $date = (new DateTime())->sub(new DateInterval('P' . $filtres['date'])); } catch (Exception $e) { Loading Loading
src/UnicaenMail/Service/Mail/MailService.php +172 −44 Original line number Diff line number Diff line Loading @@ -11,6 +11,7 @@ use Exception; use RuntimeException; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; use UnicaenMail\Entity\Db\Mail; use Laminas\Mail\Message; Loading @@ -20,25 +21,53 @@ use Laminas\Mime\Part; use Laminas\Mvc\Controller\AbstractActionController; use UnicaenMail\Exception\NotFoundConfigException; class MailService { class MailService { use ProvidesObjectManager; const BODY_TEXT_TEMPLATE = <<<EOS ----------------------------------------------------------------------- Ce mail a été redirigé. Destinataires originaux : To: %s Cc: %s Bcc: %s EOS; const BODY_HTML_TEMPLATE = <<<EOS <p>Ce mail a été redirigé.</p> <p> Destinataires originaux :<br /> To: %s<br /> Cc: %s<br /> Bcc: %s </p> EOS; private ?Mailer $mailer; public function getMailer(): ?Mailer { return $this->mailer; } public function setMailer(?Mailer $mailer): void { $this->mailer = $mailer; } private ?string $entityClass = null; private array $config = []; /** * @param string $entityClass * @return MailService Loading @@ -49,17 +78,23 @@ class MailService { return $this; } public function setConfig(array $config): void { $this->config = $config; } public function createMailEntity() : Object public function createMailEntity(): object { $entity = new $this->entityClass(); return $entity; } /** GESTION DES ENTITES *******************************************************************************************/ public function create(Mail $mail): Mail Loading @@ -69,12 +104,16 @@ class MailService { return $mail; } public function update(Mail $mail): Mail { $this->objectManager->flush($mail); return $mail; } public function delete(Mail $mail): Mail { $this->objectManager->remove($mail); Loading @@ -82,6 +121,8 @@ class MailService { return $mail; } /** REQUETAGE *****************************************************************************************************/ public function createQueryBuilder(): QueryBuilder Loading @@ -90,6 +131,8 @@ class MailService { return $qb; } /** * @param string $champ * @param string $ordre Loading @@ -104,6 +147,8 @@ class MailService { return $result; } public function getMailsByMotClef(string $motClef, string $champ = 'dateEnvoi', string $ordre = 'DESC'): array { // todo ici un fix sal au probleme de MotClef1 === MotClef123 avec MotClef1 inclu dans MotClef123 Loading @@ -116,6 +161,8 @@ class MailService { return $result; } public function getMail(?int $id): ?Mail { $qb = $this->createQueryBuilder() Loading @@ -129,6 +176,8 @@ class MailService { return $result; } public function getRequestedMail(AbstractActionController $controller, string $param = 'mail'): ?Mail { $id = $controller->params()->fromRoute($param); Loading @@ -154,6 +203,8 @@ class MailService { return $value; } public function transform(Mail $mail, ?string $module = null): Email { try { Loading Loading @@ -212,6 +263,8 @@ class MailService { return $message; } public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null, $copie = null): ?Mail { try { Loading Loading @@ -271,6 +324,79 @@ class MailService { return $mail; } public function send(Email $mail, ?string $module = null): void { $doNotSend = $this->fetchValueFromConfig('do_not_send', $module); $redirect = $this->fetchValueFromConfig('redirect', $module); if ($doNotSend) { return; } if ($redirect) { $mail = $this->prepareMessageForRedirection($mail, $module); } try { $this->getMailer()->send($mail); } catch (TransportExceptionInterface $e) { throw new RuntimeException("Échec de l'envoi du message", 0, $e); } } protected function prepareMessageForRedirection(Email $mail, ?string $module = null): Email { // On crée un clone $redirMail = clone($mail); // Redirection effective $redirMail->getHeaders()->remove('To'); // Supprime tous les To $redirMail->getHeaders()->remove('Cc'); // Supprime tous les CC $redirMail->getHeaders()->remove('Bcc'); // Supprime tous les BCC $redirectTo = (array)$this->fetchValueFromConfig('redirect_to', $module); foreach ($redirectTo as $to) { $redirMail->addTo($to); } // Modification du sujet $redirMail->subject($mail->getSubject() . " {REDIR}"); // Modification du corps $to = []; $cc = []; $bcc = []; foreach ($mail->getTo() as $addr) { $to[] = $addr->toString(); } foreach ($mail->getCc() as $addr) { $cc[] = $addr->toString(); } foreach ($mail->getBcc() as $addr) { $bcc[] = $addr->toString(); } $to = implode(", ", $to); $cc = implode(", ", $cc); $bcc = implode(", ", $bcc); if ($textBody = $mail->getTextBody()){ $textBody .= sprintf(self::BODY_TEXT_TEMPLATE, $to, $cc, $bcc); $redirMail->text($textBody); } if ($htmlBody = $mail->getHtmlBody()){ $htmlBody .= sprintf(self::BODY_HTML_TEMPLATE, $to, $cc, $bcc); $redirMail->html($htmlBody); } return $redirMail; } /** TODO : Le reenvoi ne tient pas compte du module ... */ public function reenvoi(Mail $mail): Mail { Loading @@ -285,15 +411,17 @@ class MailService { } /** @return Mail[] */ public function getMailsWithFiltre(array $filtres): array { $qb = $this->createQueryBuilder(); if (isset($filtres['statut']) AND $filtres['statut'] !== '') { if (isset($filtres['statut']) and $filtres['statut'] !== '') { $qb = $qb->andWhere('mail.statusEnvoi = :statut')->setParameter('statut', $filtres['statut']); } if (isset($filtres['date']) AND $filtres['date'] !== '') { if (isset($filtres['date']) and $filtres['date'] !== '') { try { $date = (new DateTime())->sub(new DateInterval('P' . $filtres['date'])); } catch (Exception $e) { Loading