Loading config/module.config.php +7 Changes for config/module.config.php: 7 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -2,6 +2,9 @@ namespace UnicaenApp; use UnicaenApp\Service\Mailer\MailerService; use UnicaenApp\Service\Mailer\MailerServiceFactory; return [ /** * Paramètres de fonctionnement LDAP. Loading Loading @@ -284,6 +287,7 @@ return [ 'MouchardCompleterHttp' => 'UnicaenApp\Mouchard\MouchardCompleterHttpFactory', 'MouchardCompleterMvc' => 'UnicaenApp\Mouchard\MouchardCompleterMvcFactory', MailerService::class => MailerServiceFactory::class, ], 'shared' => [ 'MouchardListenerErrorHandler' => false, Loading @@ -305,6 +309,9 @@ return [ 'initializers' => [ 'UnicaenApp\Service\EntityManagerAwareInitializer', ], 'aliases' => [ 'MailerService' => MailerService::class, ] ], 'form_elements' => [ 'invokables' => [ Loading config/unicaen-app.local.php.dist +4 −2 Changes for config/unicaen-app.local.php.dist: 4 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -33,8 +33,10 @@ return array( 'host' => 'smtp.unicaen.fr', 'port' => 25, ), // adresses à substituer à celles des destinataires originaux ('CURRENT_USER' équivaut à l'utilisateur connecté) 'redirect_to' => array('dsi.applications@unicaen.fr', /*'CURRENT_USER'*/), // adresse d'expédition des mails envoyés 'from' => 'ne_pas_repondre@domain.fr', // adresses à substituer à celles des destinataires originaux 'redirect_to' => array('dsi.applications@unicaen.fr'), // désactivation totale de l'envoi de mail par l'application 'do_not_send' => false, ), Loading src/UnicaenApp/Controller/Plugin/Mail.php +19 −188 Changes for src/UnicaenApp/Controller/Plugin/Mail.php: 19 added lines, 188 removed lines. Original line number Diff line number Diff line <?php namespace UnicaenApp\Controller\Plugin; use UnicaenApp\Service\Mailer\MailerServiceAwareTrait; use Zend\Mail\Message; use Zend\Mail\Transport\TransportInterface; use Zend\Mime\Mime; use Zend\Mime\Part; use Zend\Mime\Message as MimeMessage; /** * Description of Mail * Plugin de contrôleur d'accès au service d'envoi de mail. * * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> */ class Mail extends \Zend\Mvc\Controller\Plugin\AbstractPlugin { const SUBJECT_SUFFIX = ' {REDIR}'; const CURRENT_USER = 'CURRENT_USER'; 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; // const CURRENT_USER = 'CURRENT_USER'; /** * @var TransportInterface */ protected $transport; /** * @var array */ protected $redirectTo = array(); /** * @var bool */ protected $doNotSend = false; /** * @var \UnicaenApp\Entity\Ldap\People */ protected $identity; /** * Constructeur. * * @param TransportInterface $transport Mode de transport à utiliser */ public function __construct(TransportInterface $transport) { $this->setTransport($transport); } use MailerServiceAwareTrait; /** * * @return self */ public function __invoke() Loading @@ -81,24 +29,12 @@ EOS; * * @param string $htmlBody HTML du corps de mail * @param string $subject Sujet du mail * @param string $from Adresse de l'expéditeur, 'ne_pas_repondre@unicaen.fr' par défaut * @param string $from Adresse d'expédition à utiliser à la place de celle de la config * @return Message */ public function createNewMessage($htmlBody, $subject, $from = 'ne_pas_repondre@unicaen.fr') public function createNewMessage($htmlBody, $subject, $from = null) { // corps au format HTML $html = $htmlBody; $part = new Part($html); $part->type = Mime::TYPE_HTML; $part->charset = 'UTF-8'; $body = new MimeMessage(); $body->addPart($part); return (new Message()) ->setEncoding('UTF-8') ->setFrom($from) ->setSubject($subject) ->setBody($body); return $this->mailerService->createNewMessage($htmlBody, $subject, $from); } /** Loading @@ -109,116 +45,7 @@ EOS; */ public function send(Message $message) { $msg = $this->prepareMessage($message); if (!$this->getDoNotSend()) { $this->getTransport()->send($msg); } return $msg; } /** * * @param Message $message * @return Message */ protected function prepareMessage(Message $message) { if (!$this->getRedirectTo()) { return $message; } // collecte des destinataires originaux pour les afficher à la fin du mail $to = array(); $cc = array(); $bcc = array(); foreach ($message->getTo() as $addr) { /* @var $addr \Zend\Mail\Address */ $to[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getCc() as $addr) { /* @var $addr \Zend\Mail\Address */ $cc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getBcc() as $addr) { /* @var $addr \Zend\Mail\Address */ $bcc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } $to = implode(", ", $to); $cc = implode(", ", $cc); $bcc = implode(", ", $bcc); $body = $message->getBody(); /** * Si corps de mail en HTML */ if ($body instanceof \Zend\Mime\Message) { $template = self::BODY_HTML_TEMPLATE; $part = new Part(sprintf($template, $to, $cc, $bcc)); $part->type = Mime::TYPE_HTML; $part->charset = $message->getEncoding(); $body->addPart($part); } /** * Si corps de mail texte ou autre */ else { $template = self::BODY_TEXT_TEMPLATE; $body .= sprintf($template, $to, $cc, $bcc); } // si 'CURRENT_USER' est trouvé dans les adresses de redirection, l'utilisateur connecté est ajouté aux destinataires if (($identity = $this->getIdentity())) { foreach ($redirectTo = $this->getRedirectTo() as $key => $value) { if (self::CURRENT_USER === $key || self::CURRENT_USER === $value) { $redirectTo[$identity->getMail()] = $identity->getNomComplet(true); unset($redirectTo[$key]); } } $this->setRedirectTo($redirectTo); } $msg = new Message(); $msg->setSubject($message->getSubject() . self::SUBJECT_SUFFIX) ->setFrom($message->getFrom()) ->setTo($this->getRedirectTo()) ->setCc(array()) ->setBcc(array()) ->setBody($body) ->setEncoding($message->getEncoding()); return $msg; } /** * Retourne l'identité de l'utilisateur connecté. * * @return \UnicaenApp\Entity\Ldap\People */ public function getIdentity() { if (null === $this->identity) { try { $this->identity = $this->getController()->zfcUserAuthentication()->getIdentity(); } catch (\Zend\ServiceManager\Exception\ServiceNotFoundException $snfe) { $this->identity = null; } if (!$this->identity instanceof \UnicaenApp\Entity\Ldap\People) { $this->identity = null; } } return $this->identity; } /** * Spécifie l'identité de l'utilisateur connecté. * * @param \UnicaenApp\Entity\Ldap\People $identity * @return self */ public function setIdentity(\UnicaenApp\Entity\Ldap\People $identity) { $this->identity = $identity; return $this; return $this->mailerService->send($message); } /** Loading @@ -228,7 +55,7 @@ EOS; */ public function getTransport() { return $this->transport; return $this->mailerService->getTransport(); } /** Loading @@ -239,7 +66,8 @@ EOS; */ public function setTransport(TransportInterface $transport) { $this->transport = $transport; $this->mailerService->setTransport($transport); return $this; } Loading @@ -251,7 +79,7 @@ EOS; */ public function getRedirectTo() { return $this->redirectTo ? (array)$this->redirectTo : array(); return $this->mailerService->getRedirectTo(); } /** Loading @@ -263,7 +91,8 @@ EOS; */ public function setRedirectTo(array $redirectTo = array()) { $this->redirectTo = $redirectTo; $this->mailerService->setRedirectTo($redirectTo); return $this; } Loading @@ -275,7 +104,8 @@ EOS; */ public function addRedirectTo(array $redirectTo = array()) { $this->setRedirectTo(array_merge($this->getRedirectTo(), $redirectTo)); $this->mailerService->addRedirectTo($redirectTo); return $this; } Loading @@ -286,7 +116,7 @@ EOS; */ public function getDoNotSend() { return $this->doNotSend; return $this->mailerService->getDoNotSend(); } /** Loading @@ -297,7 +127,8 @@ EOS; */ public function setDoNotSend($doNotSend = true) { $this->doNotSend = (bool)$doNotSend; $this->mailerService->setDoNotSend($doNotSend); return $this; } } No newline at end of file src/UnicaenApp/Controller/Plugin/MailFactory.php +9 −25 Changes for src/UnicaenApp/Controller/Plugin/MailFactory.php: 9 added lines, 25 removed lines. Original line number Diff line number Diff line Loading @@ -2,43 +2,27 @@ namespace UnicaenApp\Controller\Plugin; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Mail\Transport\SmtpOptions; use Zend\Mail\Transport\Smtp; use Zend\ServiceManager\Exception\InvalidArgumentException; use UnicaenApp\Service\Mailer\MailerService; use Zend\Mvc\Controller\PluginManager; /** * Description of MailFactory * * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> */ class MailFactory implements FactoryInterface class MailFactory { /** * Create service * * @param ServiceLocatorInterface $pluginManager * @param PluginManager $pluginManager * @return Mail */ public function createService(ServiceLocatorInterface $pluginManager) public function __invoke(PluginManager $pluginManager) { $options = $pluginManager->getServiceLocator()->get('unicaen-app_module_options'); /* @var $options ModuleOptions */ $mailOptions = $options->getMail(); if (!isset($mailOptions['transport_options'])) { throw new InvalidArgumentException("Options de transport de mail introuvables."); } /** @var MailerService $mailerService */ $mailerService = $pluginManager->getServiceLocator()->get(MailerService::class); $transport = new Smtp(new SmtpOptions($mailOptions['transport_options'])); $plugin = new Mail($transport); if (!empty($mailOptions['redirect_to'])) { $plugin->setRedirectTo($mailOptions['redirect_to']); } if (isset($mailOptions['do_not_send'])) { $plugin->setDoNotSend($mailOptions['do_not_send']); } $plugin = new Mail(); $plugin->setMailerService($mailerService); return $plugin; } Loading src/UnicaenApp/Service/Mailer/MailerService.php +275 Changes for src/UnicaenApp/Service/Mailer/MailerService.php: 275 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace UnicaenApp\Service\Mailer; use UnicaenApp\Exception\LogicException; use Zend\Mail\Message; use Zend\Mail\Transport\TransportInterface; use Zend\Mime\Mime; use Zend\Mime\Part; use Zend\Mime\Message as MimeMessage; /** * Service d'envoi de mail. * * @author Unicaen */ class MailerService { const SUBJECT_REDIR_SUFFIX = ' {REDIR}'; 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; /** * @var TransportInterface */ protected $transport; /** * @var array */ protected $redirectTo = []; /** * @var bool */ protected $doNotSend = false; /** * @var string */ protected $from; /** * Constructeur. * * @param TransportInterface $transport Mode de transport à utiliser */ public function __construct(TransportInterface $transport) { $this->transport = $transport; } /** * @param string $from * @return self */ public function setFrom($from) { $this->from = $from; return $this; } /** * Méthode utilitaire pour instancier un nouveau message ayant un corps au format HTML. * Car c'est ce qu'on fait la plupart du temps dans nos applications ! * * @param string $htmlBody HTML du corps de mail * @param string $subject Sujet du mail * @param string $from Adresse d'expédition à utiliser à la place de celle de la config * @return Message */ public function createNewMessage($htmlBody, $subject, $from = null) { $from = $from ?: $this->from; // corps au format HTML $html = $htmlBody; $part = new Part($html); $part->type = Mime::TYPE_HTML; $part->charset = 'UTF-8'; $body = new MimeMessage(); $body->addPart($part); return (new Message()) ->setEncoding('UTF-8') ->setFrom($from) ->setSubject($subject) ->setBody($body); } /** * Envoie le message. * * @param Message $message Message à envoyer * @return Message Message effectivement envoyé, différent de l'original si la redirection est activée */ public function send(Message $message) { if ($this->getRedirectTo()) { $msg = $this->prepareMessageForRedirection($message); } else { $msg = $message; } if ($this->getDoNotSend()) { return $msg; } $this->transport->send($msg); return $msg; } /** * @param Message $message * @return Message */ protected function prepareMessageForRedirection(Message $message) { // collecte des destinataires originaux pour les afficher à la fin du mail $to = []; $cc = []; $bcc = []; foreach ($message->getTo() as $addr) { /* @var $addr \Zend\Mail\Address */ $to[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getCc() as $addr) { /* @var $addr \Zend\Mail\Address */ $cc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getBcc() as $addr) { /* @var $addr \Zend\Mail\Address */ $bcc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } $to = implode(", ", $to); $cc = implode(", ", $cc); $bcc = implode(", ", $bcc); $body = $message->getBody(); /** * Si corps de mail en HTML */ if ($body instanceof \Zend\Mime\Message) { $template = self::BODY_HTML_TEMPLATE; $part = new Part(sprintf($template, $to, $cc, $bcc)); $part->type = Mime::TYPE_HTML; $part->charset = $message->getEncoding(); $body->addPart($part); } /** * Si corps de mail texte ou autre */ else { $template = self::BODY_TEXT_TEMPLATE; $body .= sprintf($template, $to, $cc, $bcc); } // 'CURRENT_USER' dans les adresses de redirection n'est plus supportée foreach ($redirectTo = $this->getRedirectTo() as $key => $value) { if ('CURRENT_USER' === $key || 'CURRENT_USER' === $value) { throw new LogicException("La valeur 'CURRENT_USER' n'est plus supportée par le mécanisme de redirection de mail"); } } $msg = new Message(); $msg->setSubject($message->getSubject() . self::SUBJECT_REDIR_SUFFIX) ->setFrom($message->getFrom()) ->setTo($this->getRedirectTo()) ->setCc([]) ->setBcc([]) ->setBody($body) ->setEncoding($message->getEncoding()); return $msg; } /** * Retourne le mode de transport à utiliser. * * @return TransportInterface */ public function getTransport() { return $this->transport; } /** * Spécifie le mode de transport à utiliser. * * @param TransportInterface $transport * @return self */ public function setTransport(TransportInterface $transport) { $this->transport = $transport; return $this; } /** * Retourne les adresses vers lesquelles rediriger les mails. * NB: elles sont substituées aux adresses originales. * * @return array */ public function getRedirectTo() { return $this->redirectTo ? (array)$this->redirectTo : []; } /** * Spécifie les adresses vers lesquelles rediriger les mails. * NB: elles sont substituées aux adresses originales. * * @param array|string $redirectTo Tableau d'emails, ou emails séparés par une virgule * @return self */ public function setRedirectTo($redirectTo) { $this->redirectTo = []; $this->addRedirectTo($redirectTo); return $this; } /** * Ajoute des adresses vers lesquelles rediriger les mails. * * @param array|string $redirectTo Tableau d'emails, ou emails séparés par une virgule * @return self */ public function addRedirectTo($redirectTo) { if (is_string($redirectTo)) { $redirectTo = array_map('trim', explode(',', $redirectTo)); } $this->redirectTo = array_merge($this->redirectTo, $redirectTo); return $this; } /** * Retourne le flag indiquant si l'envoi des mails est désactivé. * * @return bool */ public function getDoNotSend() { return $this->doNotSend; } /** * Spécifie le flag indiquant si l'envoi des mails est désactivé. * * @param bool $doNotSend * @return self */ public function setDoNotSend($doNotSend = true) { $this->doNotSend = (bool)$doNotSend; return $this; } } Loading
config/module.config.php +7 Changes for config/module.config.php: 7 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -2,6 +2,9 @@ namespace UnicaenApp; use UnicaenApp\Service\Mailer\MailerService; use UnicaenApp\Service\Mailer\MailerServiceFactory; return [ /** * Paramètres de fonctionnement LDAP. Loading Loading @@ -284,6 +287,7 @@ return [ 'MouchardCompleterHttp' => 'UnicaenApp\Mouchard\MouchardCompleterHttpFactory', 'MouchardCompleterMvc' => 'UnicaenApp\Mouchard\MouchardCompleterMvcFactory', MailerService::class => MailerServiceFactory::class, ], 'shared' => [ 'MouchardListenerErrorHandler' => false, Loading @@ -305,6 +309,9 @@ return [ 'initializers' => [ 'UnicaenApp\Service\EntityManagerAwareInitializer', ], 'aliases' => [ 'MailerService' => MailerService::class, ] ], 'form_elements' => [ 'invokables' => [ Loading
config/unicaen-app.local.php.dist +4 −2 Changes for config/unicaen-app.local.php.dist: 4 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -33,8 +33,10 @@ return array( 'host' => 'smtp.unicaen.fr', 'port' => 25, ), // adresses à substituer à celles des destinataires originaux ('CURRENT_USER' équivaut à l'utilisateur connecté) 'redirect_to' => array('dsi.applications@unicaen.fr', /*'CURRENT_USER'*/), // adresse d'expédition des mails envoyés 'from' => 'ne_pas_repondre@domain.fr', // adresses à substituer à celles des destinataires originaux 'redirect_to' => array('dsi.applications@unicaen.fr'), // désactivation totale de l'envoi de mail par l'application 'do_not_send' => false, ), Loading
src/UnicaenApp/Controller/Plugin/Mail.php +19 −188 Changes for src/UnicaenApp/Controller/Plugin/Mail.php: 19 added lines, 188 removed lines. Original line number Diff line number Diff line <?php namespace UnicaenApp\Controller\Plugin; use UnicaenApp\Service\Mailer\MailerServiceAwareTrait; use Zend\Mail\Message; use Zend\Mail\Transport\TransportInterface; use Zend\Mime\Mime; use Zend\Mime\Part; use Zend\Mime\Message as MimeMessage; /** * Description of Mail * Plugin de contrôleur d'accès au service d'envoi de mail. * * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> */ class Mail extends \Zend\Mvc\Controller\Plugin\AbstractPlugin { const SUBJECT_SUFFIX = ' {REDIR}'; const CURRENT_USER = 'CURRENT_USER'; 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; // const CURRENT_USER = 'CURRENT_USER'; /** * @var TransportInterface */ protected $transport; /** * @var array */ protected $redirectTo = array(); /** * @var bool */ protected $doNotSend = false; /** * @var \UnicaenApp\Entity\Ldap\People */ protected $identity; /** * Constructeur. * * @param TransportInterface $transport Mode de transport à utiliser */ public function __construct(TransportInterface $transport) { $this->setTransport($transport); } use MailerServiceAwareTrait; /** * * @return self */ public function __invoke() Loading @@ -81,24 +29,12 @@ EOS; * * @param string $htmlBody HTML du corps de mail * @param string $subject Sujet du mail * @param string $from Adresse de l'expéditeur, 'ne_pas_repondre@unicaen.fr' par défaut * @param string $from Adresse d'expédition à utiliser à la place de celle de la config * @return Message */ public function createNewMessage($htmlBody, $subject, $from = 'ne_pas_repondre@unicaen.fr') public function createNewMessage($htmlBody, $subject, $from = null) { // corps au format HTML $html = $htmlBody; $part = new Part($html); $part->type = Mime::TYPE_HTML; $part->charset = 'UTF-8'; $body = new MimeMessage(); $body->addPart($part); return (new Message()) ->setEncoding('UTF-8') ->setFrom($from) ->setSubject($subject) ->setBody($body); return $this->mailerService->createNewMessage($htmlBody, $subject, $from); } /** Loading @@ -109,116 +45,7 @@ EOS; */ public function send(Message $message) { $msg = $this->prepareMessage($message); if (!$this->getDoNotSend()) { $this->getTransport()->send($msg); } return $msg; } /** * * @param Message $message * @return Message */ protected function prepareMessage(Message $message) { if (!$this->getRedirectTo()) { return $message; } // collecte des destinataires originaux pour les afficher à la fin du mail $to = array(); $cc = array(); $bcc = array(); foreach ($message->getTo() as $addr) { /* @var $addr \Zend\Mail\Address */ $to[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getCc() as $addr) { /* @var $addr \Zend\Mail\Address */ $cc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getBcc() as $addr) { /* @var $addr \Zend\Mail\Address */ $bcc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } $to = implode(", ", $to); $cc = implode(", ", $cc); $bcc = implode(", ", $bcc); $body = $message->getBody(); /** * Si corps de mail en HTML */ if ($body instanceof \Zend\Mime\Message) { $template = self::BODY_HTML_TEMPLATE; $part = new Part(sprintf($template, $to, $cc, $bcc)); $part->type = Mime::TYPE_HTML; $part->charset = $message->getEncoding(); $body->addPart($part); } /** * Si corps de mail texte ou autre */ else { $template = self::BODY_TEXT_TEMPLATE; $body .= sprintf($template, $to, $cc, $bcc); } // si 'CURRENT_USER' est trouvé dans les adresses de redirection, l'utilisateur connecté est ajouté aux destinataires if (($identity = $this->getIdentity())) { foreach ($redirectTo = $this->getRedirectTo() as $key => $value) { if (self::CURRENT_USER === $key || self::CURRENT_USER === $value) { $redirectTo[$identity->getMail()] = $identity->getNomComplet(true); unset($redirectTo[$key]); } } $this->setRedirectTo($redirectTo); } $msg = new Message(); $msg->setSubject($message->getSubject() . self::SUBJECT_SUFFIX) ->setFrom($message->getFrom()) ->setTo($this->getRedirectTo()) ->setCc(array()) ->setBcc(array()) ->setBody($body) ->setEncoding($message->getEncoding()); return $msg; } /** * Retourne l'identité de l'utilisateur connecté. * * @return \UnicaenApp\Entity\Ldap\People */ public function getIdentity() { if (null === $this->identity) { try { $this->identity = $this->getController()->zfcUserAuthentication()->getIdentity(); } catch (\Zend\ServiceManager\Exception\ServiceNotFoundException $snfe) { $this->identity = null; } if (!$this->identity instanceof \UnicaenApp\Entity\Ldap\People) { $this->identity = null; } } return $this->identity; } /** * Spécifie l'identité de l'utilisateur connecté. * * @param \UnicaenApp\Entity\Ldap\People $identity * @return self */ public function setIdentity(\UnicaenApp\Entity\Ldap\People $identity) { $this->identity = $identity; return $this; return $this->mailerService->send($message); } /** Loading @@ -228,7 +55,7 @@ EOS; */ public function getTransport() { return $this->transport; return $this->mailerService->getTransport(); } /** Loading @@ -239,7 +66,8 @@ EOS; */ public function setTransport(TransportInterface $transport) { $this->transport = $transport; $this->mailerService->setTransport($transport); return $this; } Loading @@ -251,7 +79,7 @@ EOS; */ public function getRedirectTo() { return $this->redirectTo ? (array)$this->redirectTo : array(); return $this->mailerService->getRedirectTo(); } /** Loading @@ -263,7 +91,8 @@ EOS; */ public function setRedirectTo(array $redirectTo = array()) { $this->redirectTo = $redirectTo; $this->mailerService->setRedirectTo($redirectTo); return $this; } Loading @@ -275,7 +104,8 @@ EOS; */ public function addRedirectTo(array $redirectTo = array()) { $this->setRedirectTo(array_merge($this->getRedirectTo(), $redirectTo)); $this->mailerService->addRedirectTo($redirectTo); return $this; } Loading @@ -286,7 +116,7 @@ EOS; */ public function getDoNotSend() { return $this->doNotSend; return $this->mailerService->getDoNotSend(); } /** Loading @@ -297,7 +127,8 @@ EOS; */ public function setDoNotSend($doNotSend = true) { $this->doNotSend = (bool)$doNotSend; $this->mailerService->setDoNotSend($doNotSend); return $this; } } No newline at end of file
src/UnicaenApp/Controller/Plugin/MailFactory.php +9 −25 Changes for src/UnicaenApp/Controller/Plugin/MailFactory.php: 9 added lines, 25 removed lines. Original line number Diff line number Diff line Loading @@ -2,43 +2,27 @@ namespace UnicaenApp\Controller\Plugin; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Mail\Transport\SmtpOptions; use Zend\Mail\Transport\Smtp; use Zend\ServiceManager\Exception\InvalidArgumentException; use UnicaenApp\Service\Mailer\MailerService; use Zend\Mvc\Controller\PluginManager; /** * Description of MailFactory * * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> */ class MailFactory implements FactoryInterface class MailFactory { /** * Create service * * @param ServiceLocatorInterface $pluginManager * @param PluginManager $pluginManager * @return Mail */ public function createService(ServiceLocatorInterface $pluginManager) public function __invoke(PluginManager $pluginManager) { $options = $pluginManager->getServiceLocator()->get('unicaen-app_module_options'); /* @var $options ModuleOptions */ $mailOptions = $options->getMail(); if (!isset($mailOptions['transport_options'])) { throw new InvalidArgumentException("Options de transport de mail introuvables."); } /** @var MailerService $mailerService */ $mailerService = $pluginManager->getServiceLocator()->get(MailerService::class); $transport = new Smtp(new SmtpOptions($mailOptions['transport_options'])); $plugin = new Mail($transport); if (!empty($mailOptions['redirect_to'])) { $plugin->setRedirectTo($mailOptions['redirect_to']); } if (isset($mailOptions['do_not_send'])) { $plugin->setDoNotSend($mailOptions['do_not_send']); } $plugin = new Mail(); $plugin->setMailerService($mailerService); return $plugin; } Loading
src/UnicaenApp/Service/Mailer/MailerService.php +275 Changes for src/UnicaenApp/Service/Mailer/MailerService.php: 275 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace UnicaenApp\Service\Mailer; use UnicaenApp\Exception\LogicException; use Zend\Mail\Message; use Zend\Mail\Transport\TransportInterface; use Zend\Mime\Mime; use Zend\Mime\Part; use Zend\Mime\Message as MimeMessage; /** * Service d'envoi de mail. * * @author Unicaen */ class MailerService { const SUBJECT_REDIR_SUFFIX = ' {REDIR}'; 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; /** * @var TransportInterface */ protected $transport; /** * @var array */ protected $redirectTo = []; /** * @var bool */ protected $doNotSend = false; /** * @var string */ protected $from; /** * Constructeur. * * @param TransportInterface $transport Mode de transport à utiliser */ public function __construct(TransportInterface $transport) { $this->transport = $transport; } /** * @param string $from * @return self */ public function setFrom($from) { $this->from = $from; return $this; } /** * Méthode utilitaire pour instancier un nouveau message ayant un corps au format HTML. * Car c'est ce qu'on fait la plupart du temps dans nos applications ! * * @param string $htmlBody HTML du corps de mail * @param string $subject Sujet du mail * @param string $from Adresse d'expédition à utiliser à la place de celle de la config * @return Message */ public function createNewMessage($htmlBody, $subject, $from = null) { $from = $from ?: $this->from; // corps au format HTML $html = $htmlBody; $part = new Part($html); $part->type = Mime::TYPE_HTML; $part->charset = 'UTF-8'; $body = new MimeMessage(); $body->addPart($part); return (new Message()) ->setEncoding('UTF-8') ->setFrom($from) ->setSubject($subject) ->setBody($body); } /** * Envoie le message. * * @param Message $message Message à envoyer * @return Message Message effectivement envoyé, différent de l'original si la redirection est activée */ public function send(Message $message) { if ($this->getRedirectTo()) { $msg = $this->prepareMessageForRedirection($message); } else { $msg = $message; } if ($this->getDoNotSend()) { return $msg; } $this->transport->send($msg); return $msg; } /** * @param Message $message * @return Message */ protected function prepareMessageForRedirection(Message $message) { // collecte des destinataires originaux pour les afficher à la fin du mail $to = []; $cc = []; $bcc = []; foreach ($message->getTo() as $addr) { /* @var $addr \Zend\Mail\Address */ $to[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getCc() as $addr) { /* @var $addr \Zend\Mail\Address */ $cc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } foreach ($message->getBcc() as $addr) { /* @var $addr \Zend\Mail\Address */ $bcc[] = $addr->getEmail() . ($addr->getName() ? ' <' . $addr->getName() . '>' : null); } $to = implode(", ", $to); $cc = implode(", ", $cc); $bcc = implode(", ", $bcc); $body = $message->getBody(); /** * Si corps de mail en HTML */ if ($body instanceof \Zend\Mime\Message) { $template = self::BODY_HTML_TEMPLATE; $part = new Part(sprintf($template, $to, $cc, $bcc)); $part->type = Mime::TYPE_HTML; $part->charset = $message->getEncoding(); $body->addPart($part); } /** * Si corps de mail texte ou autre */ else { $template = self::BODY_TEXT_TEMPLATE; $body .= sprintf($template, $to, $cc, $bcc); } // 'CURRENT_USER' dans les adresses de redirection n'est plus supportée foreach ($redirectTo = $this->getRedirectTo() as $key => $value) { if ('CURRENT_USER' === $key || 'CURRENT_USER' === $value) { throw new LogicException("La valeur 'CURRENT_USER' n'est plus supportée par le mécanisme de redirection de mail"); } } $msg = new Message(); $msg->setSubject($message->getSubject() . self::SUBJECT_REDIR_SUFFIX) ->setFrom($message->getFrom()) ->setTo($this->getRedirectTo()) ->setCc([]) ->setBcc([]) ->setBody($body) ->setEncoding($message->getEncoding()); return $msg; } /** * Retourne le mode de transport à utiliser. * * @return TransportInterface */ public function getTransport() { return $this->transport; } /** * Spécifie le mode de transport à utiliser. * * @param TransportInterface $transport * @return self */ public function setTransport(TransportInterface $transport) { $this->transport = $transport; return $this; } /** * Retourne les adresses vers lesquelles rediriger les mails. * NB: elles sont substituées aux adresses originales. * * @return array */ public function getRedirectTo() { return $this->redirectTo ? (array)$this->redirectTo : []; } /** * Spécifie les adresses vers lesquelles rediriger les mails. * NB: elles sont substituées aux adresses originales. * * @param array|string $redirectTo Tableau d'emails, ou emails séparés par une virgule * @return self */ public function setRedirectTo($redirectTo) { $this->redirectTo = []; $this->addRedirectTo($redirectTo); return $this; } /** * Ajoute des adresses vers lesquelles rediriger les mails. * * @param array|string $redirectTo Tableau d'emails, ou emails séparés par une virgule * @return self */ public function addRedirectTo($redirectTo) { if (is_string($redirectTo)) { $redirectTo = array_map('trim', explode(',', $redirectTo)); } $this->redirectTo = array_merge($this->redirectTo, $redirectTo); return $this; } /** * Retourne le flag indiquant si l'envoi des mails est désactivé. * * @return bool */ public function getDoNotSend() { return $this->doNotSend; } /** * Spécifie le flag indiquant si l'envoi des mails est désactivé. * * @param bool $doNotSend * @return self */ public function setDoNotSend($doNotSend = true) { $this->doNotSend = (bool)$doNotSend; return $this; } }