Commit 8337e6ed authored by David Surville's avatar David Surville
Browse files

- [Fix] Possibilité d'utiliser un tableau au niveau des paramètres $to,...

- [Fix] Possibilité d'utiliser un tableau au niveau des paramètres $to, $copie, $reply_to, $attachment_path de la fonction MailService::sendMail ce qui était le cas avant la version 7.1
- Ajout d'un paramètre supplémentaire $message_auto à la fonction MailService::sendMail pour afficher ou non le texte d'envoi automatique
parent 69bc236c
Loading
Loading
Loading
Loading
Loading
+59 −27
Original line number Diff line number Diff line
@@ -189,7 +189,7 @@ EOS;
        if ($value === null and isset($config[$key])) $value = $config[$key];

        if (!$optional and $value === null) {
            throw new NotFoundConfigException("Aucun valeur de trouver dans la configuration de UnicaenMail pour la clef [" . $key . "]");
            throw new NotFoundConfigException("Aucun valeur trouvée dans la configuration de UnicaenMail pour la clef [" . $key . "]");
        }
        return $value;
    }
@@ -203,33 +203,37 @@ EOS;
            $fromName = $this->fetchValueFromConfig('from_name', $module);
            $redirect = $this->fetchValueFromConfig('redirect', $module, true);
            $replyTo = $this->fetchValueFromConfig('reply_to', $module, true);
        } catch (NotFoundConfigException $e) {
        }
        catch (NotFoundConfigException $e) {
            throw new RuntimeException("Un problème est survenu lors de la récupération de valeurs de config", 0, $e);
        }

        $mailSymfony = new Email();
        $mailSymfony->from("'\"" . $fromName . "\" <" . $fromEmail . ">'");
        $adresseToArray = $redirect ? explode(',', $mail->getDestinatairesInitials()) : (explode(',', $mail->getDestinataires()));
        $adresseToArray = $redirect ? explode(',', $mail->getDestinatairesInitials()) : explode(',', $mail->getDestinataires());
        $mailSymfony->to(...$adresseToArray);
        if ($mail->getCopies() !== null and $mail->getCopies() !== '') {

        if ($mail->getCopies() != null) {
            $adresseCcArray = explode(',', $mail->getCopies());
            $mailSymfony->cc(...$adresseCcArray);
        }
        if ($mail->getReplyTo() !== null or $replyTo !== null) {
            $mailSymfony->replyTo($mail->getReplyTo() ?? $replyTo);
        if ($mail->getReplyTo() != null OR $replyTo != null) {
            $mailSymfony->replyTo($mail->getReplyTo() ?: $replyTo);
        }

        $mailSymfony->subject($mail->getSujet());
        $mailSymfony->html($mail->getCorps());
        $mailSymfony->text(strip_tags($mail->getCorps()));
        //if ($mail->getAttachmentPaths()) $mailSymfony->attachFromPath($mail->getAttachmentPaths());
        if ($mail->getAttachmentPaths() !== null) {

        if ($mail->getAttachmentPaths() != null) {
            $attachements = explode(MailService::ATTACHMENT_SEP, $mail->getAttachmentPaths());
            foreach ($attachements as $attachement) {
                $extension = pathinfo($attachement, PATHINFO_EXTENSION);
                $basename = pathinfo($attachement, PATHINFO_BASENAME);
                if ($extension === 'ics') {
                switch ($extension) {
                    case 'ics':
                        $mailSymfony->attachFromPath($attachement, $basename, 'text/calendar; METHOD=REQUEST');
                } else {
                    default:
                        $mailSymfony->attachFromPath($attachement, $basename,);
                }
            }
@@ -237,22 +241,38 @@ EOS;
        return $mailSymfony;
    }

    public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null, $copie = null, $replyto = null): ?Mail
    /**
     * @param string|array $to
     * @param string $subject
     * @param string $texte
     * @param string|null $module
     * @param string|array|null $attachement_path
     * @param string|array|null $copie
     * @param string|array|null $reply_to
     * @param bool $message_auto
     * @return Mail|null
     */
    public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null, $copie = null, $reply_to = null, $message_auto = true): ?Mail
    {
        try {
            $doNotSend = $this->fetchValueFromConfig('do_not_send', $module);
            $redirect = $this->fetchValueFromConfig('redirect', $module);
            $redirectTo = (array) $this->fetchValueFromConfig('redirect_to', $module);
            $subjectPrefix = $this->fetchValueFromConfig('subject_prefix', $module);
            $replyto = ($replyto === null) ? $this->fetchValueFromConfig('reply_to', $module, true) : $replyto;
        } catch (NotFoundConfigException $e) {
            $replyTo = (!$reply_to) ? $this->fetchValueFromConfig('reply_to', $module, true) : $reply_to;
        } 
        catch (NotFoundConfigException $e) {
            throw new RuntimeException("Un problème est survenu lors de la récupération de valeurs de config", 0, $e);
        }

        if ($to === null or $to === "" or $to === []) {
        if (empty($to)) {
            return null;
        }

        if(is_array($to)) {
            $to = implode(',', $to);
        }

        /** @var Mail $mail */
        $mail = $this->createMailEntity();
        $mail->setDateEnvoi(new DateTime());
@@ -263,17 +283,24 @@ EOS;
        } else {
            $mail->setDestinataires($to);
        }
        if ($copie !== null and !empty($copie)) $mail->setCopies($copie);
        if ($replyto !== null and !empty($replyto)) $mail->setReplyTo($replyto);

        $sujet = "[" . $subjectPrefix . "] " . $subject;
        $mail->setSujet($sujet);
        $corps = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application " . $subjectPrefix . ". </i></p>" . $texte;
        if (!empty($copie)) {
            $mail->setCopies(is_array($copie) ? implode(',', $copie) : $copie);
        }
        if (!empty($replyTo)) {
            $mail->setReplyTo(is_array($replyTo) ? implode(',', $replyTo) : $replyTo);
        }

        $mail->setSujet($subjectPrefix ? sprintf("[%s] $subject", $subjectPrefix) : $subject);
        $corps = ($message_auto && $subjectPrefix)
            ? sprintf("<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application %s</i></p>%s", $subjectPrefix, $texte)
            : $texte;
        $mail->setCorps($corps);
        if ($attachement_path !== null) {
            $attachement_path = (is_string($attachement_path)) ? $attachement_path : implode(MailService::ATTACHMENT_SEP, $attachement_path);
            $mail->setAttachmentPaths($attachement_path);

        if (!empty($attachement_path)) {
            $mail->setAttachmentPaths(is_array($attachement_path) ? implode(MailService::ATTACHMENT_SEP, $attachement_path) : $attachement_path);
        }

        $this->create($mail);

        if ($doNotSend !== true) {
@@ -283,14 +310,19 @@ EOS;
                    $mailSymfony = $this->prepareMessageForRedirection($mailSymfony, $module);
                }
                $this->getMailer()->send($mailSymfony);
            } catch (TransportExceptionInterface $e) {
            }
            catch (TransportExceptionInterface $e) {
                throw new RuntimeException("Échec de l'envoi du message", 0, $e);
            }

            $mail->setStatusEnvoi(Mail::SUCCESS);
        } else {
        }
        else {
            $mail->setStatusEnvoi(Mail::NOTSENT);
        }

        $this->update($mail);

        return $mail;
    }