Commit 4e9b70de authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Correction redirection et meilleur gestion des clefs de configs

parent cd536e6f
Loading
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenMail\Exception;

use Exception;

class NotFoundConfigException extends Exception {}
 No newline at end of file
+37 −53
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ use Laminas\Mime\Message as MimeMessage;
use Laminas\Mime\Mime;
use Laminas\Mime\Part;
use Laminas\Mvc\Controller\AbstractActionController;
use UnicaenMail\Exception\NotFoundConfigException;

/**
 * @property EntityManager $objectManager
@@ -48,45 +49,10 @@ class MailService {
    }

    private ?TransportInterface $transport;
    private array $redirectTo;
    private bool $doNotSend;

    /** information sur l'expediteur */
    private ?string $fromName;
    private ?string $fromEmail;
    private ?string $subjectPrefix;

    public function __construct(TransportInterface $transport, $redirectTo, $doNotSend, $fromName, $fromEmail, $subjectPrefix)
    public function __construct(TransportInterface $transport)
    {
        $this->transport = $transport;
        $this->redirectTo = $redirectTo;
        $this->doNotSend = $doNotSend;
        $this->fromName=$fromName;
        $this->fromEmail=$fromEmail;
        $this->subjectPrefix=$subjectPrefix;
    }

    public function setRedirectTo(?array $redirectTo): void
    {
        $this->redirectTo = $redirectTo;
    }

    public function setDoNotSend(bool $doNotSend): void
    {
        $this->doNotSend = $doNotSend;
    }

    public function setFromName(?string $fromName): void
    {
        $this->fromName = $fromName;
    }
    public function setFromEmail(?string $fromEmail): void
    {
        $this->fromEmail = $fromEmail;
    }
    public function setSubjectPrefix(?string $subjectPrefix): void
    {
        $this->subjectPrefix = $subjectPrefix;
    }

    /** GESTION DES ENTITES *******************************************************************************************/
@@ -162,7 +128,7 @@ class MailService {
        try {
            $result = $qb->getQuery()->getOneOrNullResult();
        } catch (NonUniqueResultException $e) {
            throw new RuntimeException("Plusieurs Mail partagent le même id [".$id."]");
            throw new RuntimeException("Plusieurs Mail partagent le même id [".$id."]",0,$e);
        }
        return $result;
    }
@@ -177,15 +143,32 @@ class MailService {
    /** FACADE ********************************************************************************************************/

    /**
     * @param $to
     * @param $subject
     * @param $texte
     * @param string $module
     * @param null $attachement_path
     * @return Mail
     * @throws NotFoundConfigException
     */
    public function fetchValueFromConfig(string $key, ?string $module = null)
    {
        $value = ($module)?$this->config['module'][$module][$key]:null;
        if ($value === null) $value = $this->config['default'][$key];
        if ($value === null) $value = $this->config[$key];

        if ($value === null) {
            throw new NotFoundConfigException("Aucun valeur de trouver dans la configuration de UnicaenMail pour la clef [".$key."]");
        }
        return $value;
    }

    /**
     * @throws NotFoundConfigException
     */
    public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null) : Mail
    {
        $fromEmail = $this->fetchValueFromConfig('from_email', $module);
        $fromName = $this->fetchValueFromConfig('from_name', $module);
        $doNotSend = $this->fetchValueFromConfig('do_not_send', $module);
        $redirectTo = $this->fetchValueFromConfig('redirect_to', $module);
        $subjectPrefix = $this->fetchValueFromConfig('subject_prefix', $module);


        if (is_string($to)) $to=explode(',', $to);
        if (!is_array($to)) $to = [$to];

@@ -194,21 +177,16 @@ class MailService {
        $mail->setDateEnvoi(new DateTime());
        $mail->setStatusEnvoi(Mail::PENDING);
        $mail->setDestinataires(is_array($to) ? implode(",", $to) : $to);
        $initialTo = $to;
        if ($this->doNotSend) {
        if ($doNotSend) {
            $mail->setDestinatairesInitials(implode(",",$to));
            $mail->setDestinataires(implode(",",$this->redirectTo));
            $to = $this->redirectTo;
            $mail->setDestinataires(implode(",", $redirectTo));
        } else {
            $mail->setDestinataires(implode(",",$to));
        }
        $mail->setSujet($subject);

        $fromEmail = ($module)?$this->config['module'][$module]['from_email']:$this->config['default']['from_email'];
        $fromName = ($module)?$this->config['module'][$module]['from_name']:$this->config['default']['from_name'];
        $doNotSend = ($module)?$this->config['module'][$module]['do_not_send']:$this->config['default']['do_not_send'];

        $sujet = '['.$this->subjectPrefix.'] ' . $subject;
        $sujet = '['.$subjectPrefix.'] ' . $subject;
        if ($doNotSend) {
            $sujet .= ' {REDIR}';
        }
@@ -218,7 +196,7 @@ class MailService {
        $message->setFrom($fromEmail, $fromName);
        $message->setTo($to);
        $message->setSubject($sujet);
        $texte = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application ".$this->subjectPrefix.". </i></p>" . $texte;
        $texte = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application ".$subjectPrefix.". </i></p>" . $texte;
        $mail->setCorps($texte);
        $this->create($mail);

@@ -254,7 +232,12 @@ class MailService {
        $body->setParts($parts);
        $message->setBody($body);

        die();
//        var_dump($fromEmail);
//        var_dump($fromName);
//        var_dump($doNotSend);
//        var_dump($redirectTo);
//        var_dump($subjectPrefix);
//        die();
        $this->transport->send($message);

        $mail->setStatusEnvoi(Mail::SUCCESS);
@@ -263,6 +246,7 @@ class MailService {
        return $mail;
    }

    /** TODO : Le reenvoi ne tient pas compte du module ... */
    public function reenvoi(Mail $mail) : Mail
    {
        //todo les pieces jointes