Commit 56665c44 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Correction de l'envoi des pièces jointes

parent 29577e8b
Loading
Loading
Loading
Loading
Loading
+25 −11
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use RuntimeException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use UnicaenMail\Entity\Db\Mail;
use UnicaenMail\Exception\NotFoundConfigException;

@@ -20,6 +21,8 @@ class MailService
{
    use ProvidesObjectManager;

    const ATTACHMENT_SEP = "#<>#";

    const BODY_TEXT_TEMPLATE = <<<EOS

-----------------------------------------------------------------------
@@ -182,10 +185,10 @@ EOS;
        $value = null;
        $config = $this->config['module'];
        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 and isset($config['default'][$key])) $value = $config['default'][$key];
        if ($value === null and isset($config[$key])) $value = $config[$key];

        if (!$optional AND $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;
@@ -208,18 +211,29 @@ EOS;
        $mailSymfony->from("'\"" . $fromName . "\" <" . $fromEmail . ">'");
        $adresseToArray = $redirect ? explode(',', $mail->getDestinatairesInitials()) : (explode(',', $mail->getDestinataires()));
        $mailSymfony->to(...$adresseToArray);
        if ($mail->getCopies() !== null AND $mail->getCopies() !== '') {
        if ($mail->getCopies() !== null and $mail->getCopies() !== '') {
            $adresseCcArray = explode(',', $mail->getCopies());
            $mailSymfony->cc(...$adresseCcArray);
        }
        if ($mail->getReplyTo() !== null OR $replyTo !== null) {
        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()) $mailSymfony->attachFromPath($mail->getAttachmentPaths());
        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') {
                    $mailSymfony->attachFromPath($attachement, $basename, 'text/calendar');
                } else {
                    $mailSymfony->attachFromPath($attachement, $basename,);
                }
            }
        }
        return $mailSymfony;
    }

@@ -257,7 +271,7 @@ EOS;
        $corps = "<p><i>Ce courrier électronique vous a été adressé <strong>automatiquement</strong> par l'application " . $subjectPrefix . ". </i></p>" . $texte;
        $mail->setCorps($corps);
        if ($attachement_path !== null) {
            $attachement_path = (is_string($attachement_path)) ? $attachement_path : implode("#<>#", $attachement_path);
            $attachement_path = (is_string($attachement_path)) ? $attachement_path : implode(MailService::ATTACHMENT_SEP, $attachement_path);
            $mail->setAttachmentPaths($attachement_path);
        }
        $this->create($mail);