Commit 1ab1e73a authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Ajout du reply to

parent 12c1c629
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ status_envoi varchar(256) not null,
destinataires text not null,
destinataires_initials text,
copies text,
reply_to text,
sujet text,
corps text,
mots_clefs text,
+12 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ class Mail {
    private ?string $destinataires = null;
    private ?string $destinatairesInitials = null;
    private ?string $copies = null;
    private ?string $replyTo = null;
    private ?string $attachmentPaths = null;

    /** Contenu *******************************************************************************************************/
@@ -85,6 +86,16 @@ class Mail {
        $this->copies = $copies;
    }

    public function getReplyTo(): ?string
    {
        return $this->replyTo;
    }

    public function setReplyTo(?string $replyTo): void
    {
        $this->replyTo = $replyTo;
    }

    public function isRedirection() : bool
    {
        return $this->destinatairesInitials !== null;
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
        <field name="destinataires" length="256" column="destinataires"/>
        <field name="destinatairesInitials" length="256" column="destinataires_initials" nullable="true"/>
        <field name="copies" type="text" column="copies" nullable="true"/>
        <field name="replyTo" type="text" column="reply_to" nullable="true"/>
        <field name="sujet" length="9999" column="sujet" nullable="true"/>
        <field name="corps" length="9999" column="corps" nullable="true"/>
        <field name="motsClefs" length="9999" column="mots_clefs" nullable="true"/>
+26 −15
Original line number Diff line number Diff line
@@ -8,10 +8,6 @@ use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use DoctrineModule\Persistence\ProvidesObjectManager;
use Exception;
use Laminas\Mail\Message;
use Laminas\Mime\Message as MimeMessage;
use Laminas\Mime\Mime;
use Laminas\Mime\Part;
use Laminas\Mvc\Controller\AbstractActionController;
use RuntimeException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
@@ -32,6 +28,7 @@ Destinataires originaux :
To: %s
Cc: %s
Bcc: %s
Reply To: %s
EOS;
    const BODY_HTML_TEMPLATE = <<<EOS
<p>Ce mail a été redirigé.</p>
@@ -39,7 +36,8 @@ EOS;
Destinataires originaux :<br />
To: %s<br />
Cc: %s<br />
Bcc: %s
Bcc: %s<br />
Reply To: %s
</p>
EOS;

@@ -175,14 +173,15 @@ EOS;
    /**
     * @throws NotFoundConfigException
     */
    public function fetchValueFromConfig(string $key, ?string $module = null)
    public function fetchValueFromConfig(string $key, ?string $module = null, bool $optional = false)
    {
        $value = null;
        $config = $this->config['module'];
        $value = ($module) ? $config[$module][$key] : null;
        if ($value === null) $value = $config['default'][$key];
        if ($value === null) $value = $config[$key];
        if (isset($config[$module][$key])) $value = ($module) ? $config[$module][$key] : null;
        if ($value === null AND isset($config['default'][$key])) $value = $config['default'][$key];
        if ($value === null AND isset($config[$key])) $value = $config[$key];

        if ($value === null) {
        if (!$optional AND $value === null) {
            throw new NotFoundConfigException("Aucun valeur de trouver dans la configuration de UnicaenMail pour la clef [" . $key . "]");
        }
        return $value;
@@ -195,7 +194,8 @@ EOS;
        try {
            $fromEmail = $this->fetchValueFromConfig('from_email', $module);
            $fromName = $this->fetchValueFromConfig('from_name', $module);
            $redirect = $this->fetchValueFromConfig('redirect', $module);
            $redirect = $this->fetchValueFromConfig('redirect', $module, true);
            $replyTo = $this->fetchValueFromConfig('reply_to', $module, true);
        } catch (NotFoundConfigException $e) {
            throw new RuntimeException("Un problème est survenu lors de la récupération de valeurs de config", 0, $e);
        }
@@ -204,10 +204,13 @@ EOS;
        $mailSymfony->from("'\"" . $fromName . "\" <" . $fromEmail . ">'");
        $adresseToArray = $redirect ? explode(',', $mail->getDestinatairesInitials()) : (explode(',', $mail->getDestinataires()));
        $mailSymfony->to(...$adresseToArray);
        if ($mail->getCopies() !== null) {
        if ($mail->getCopies() !== null AND $mail->getCopies() !== '') {
            $adresseCcArray = explode(',', $mail->getCopies());
            $mailSymfony->cc(...$adresseCcArray);
        }
        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()));
@@ -215,13 +218,14 @@ EOS;
        return $mailSymfony;
    }

    public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null, $copie = null): ?Mail
    public function sendMail($to, $subject, $texte, ?string $module = null, $attachement_path = null, $copie = null, $replyto = null): ?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) {
            throw new RuntimeException("Un problème est survenu lors de la récupération de valeurs de config", 0, $e);
        }
@@ -241,6 +245,7 @@ EOS;
            $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);
@@ -300,6 +305,7 @@ EOS;
        $redirMail->getHeaders()->remove('To');  // Supprime tous les To
        $redirMail->getHeaders()->remove('Cc');  // Supprime tous les CC
        $redirMail->getHeaders()->remove('Bcc'); // Supprime tous les BCC
        $redirMail->getHeaders()->remove('Reply-To'); // Supprime tous les BCC

        $redirectTo = (array)$this->fetchValueFromConfig('redirect_to', $module);
        foreach ($redirectTo as $to) {
@@ -313,6 +319,7 @@ EOS;
        $to = [];
        $cc = [];
        $bcc = [];
        $replyTo = [];
        foreach ($mail->getTo() as $addr) {
            $to[] = $addr->toString();
        }
@@ -322,16 +329,20 @@ EOS;
        foreach ($mail->getBcc() as $addr) {
            $bcc[] = $addr->toString();
        }
        foreach ($mail->getReplyTo() as $addr) {
            $replyTo[] = $addr->toString();
        }
        $to = implode(", ", $to);
        $cc = implode(", ", $cc);
        $bcc = implode(", ", $bcc);
        $replyTo = implode(", ", $replyTo);

        if ($textBody = $mail->getTextBody()) {
            $textBody .= sprintf(self::BODY_TEXT_TEMPLATE, $to, $cc, $bcc);
            $textBody .= sprintf(self::BODY_TEXT_TEMPLATE, $to, $cc, $bcc, $replyTo);
            $redirMail->text($textBody);
        }
        if ($htmlBody = $mail->getHtmlBody()) {
            $htmlBody .= sprintf(self::BODY_HTML_TEMPLATE, $to, $cc, $bcc);
            $htmlBody .= sprintf(self::BODY_HTML_TEMPLATE, $to, $cc, $bcc, $replyTo);
            $redirMail->html($htmlBody);
        }