From 3529c7b03749b09ce4490b6e0ca478b84410e428 Mon Sep 17 00:00:00 2001 From: gauthierb <gauthierb@d57fa8bc-6af1-4de9-8b7d-78e900e231e7> Date: Tue, 31 Mar 2015 10:04:32 +0000 Subject: [PATCH] Envoi de mails : ajout de logs. --- data/mail.log | 0 module/Application/Module.php | 3 ++ .../Controller/NotificationController.php | 10 ++++ .../Controller/Plugin/MailWithLogPlugin.php | 45 ++++++++++++++++ .../Plugin/MailWithLogPluginFactory.php | 53 +++++++++++++++++++ public/index.php | 2 + 6 files changed, 113 insertions(+) create mode 100644 data/mail.log create mode 100644 module/Application/src/Application/Controller/Plugin/MailWithLogPlugin.php create mode 100644 module/Application/src/Application/Controller/Plugin/MailWithLogPluginFactory.php diff --git a/data/mail.log b/data/mail.log new file mode 100644 index 0000000000..e69de29bb2 diff --git a/module/Application/Module.php b/module/Application/Module.php index 66b129b150..27113a48ad 100755 --- a/module/Application/Module.php +++ b/module/Application/Module.php @@ -162,6 +162,9 @@ class Module implements ControllerPluginProviderInterface, ViewHelperProviderInt 'em' => 'Application\Controller\Plugin\Em', 'context' => 'Application\Controller\Plugin\Context', ), + 'factories' => array( + 'mail' => 'Application\Controller\Plugin\MailWithLogPluginFactory', + ), ); } diff --git a/module/Application/src/Application/Controller/NotificationController.php b/module/Application/src/Application/Controller/NotificationController.php index 740ceb0d59..f28a50fc55 100644 --- a/module/Application/src/Application/Controller/NotificationController.php +++ b/module/Application/src/Application/Controller/NotificationController.php @@ -47,6 +47,16 @@ class NotificationController extends AbstractActionController implements Context ->setVariable('nis', $nis) ->setVariable('serviceIndicateur', $this->getServiceIndicateur()); +// // init +// $message = new MailMessage(); +// $message->setEncoding('UTF-8') +// ->setFrom('ne_pas_repondre@unicaen.fr', "Application " . ($app = $this->appInfos()->getNom())) +// ->setSubject(sprintf("[%s Test]", $app)) +// ->setBody("test") +// ->addTo("bertrand.gauthier@unicaen.fr"); +// +// $this->mail()->send($message); + return $viewModel; } diff --git a/module/Application/src/Application/Controller/Plugin/MailWithLogPlugin.php b/module/Application/src/Application/Controller/Plugin/MailWithLogPlugin.php new file mode 100644 index 0000000000..2e7e8f28c7 --- /dev/null +++ b/module/Application/src/Application/Controller/Plugin/MailWithLogPlugin.php @@ -0,0 +1,45 @@ +<?php +namespace Application\Controller\Plugin; + +use Zend\Log\Logger; +use UnicaenApp\Controller\Plugin\Mail; + +/** + * Description of Mail + * + * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> + */ +class MailWithLogPlugin extends Mail +{ + protected $logger; + + public function setLogger(Logger $logger) + { + $this->logger = $logger; + return $this; + } + + /** + * Envoit le message. + * + * @param \Zend\Mail\Message $message Message à envoyer + * @return \Zend\Mail\Message Message effectivement envoyé, différent de l'original si la redirection est activée + */ + public function send(\Zend\Mail\Message $message) + { + if ($this->logger) { + $template = <<<EOS +Will send message : +................................................................................ +%s +................................................................................ + + + +EOS; + $this->logger->info(sprintf($template, $message->toString())); + } + + return parent::send($message); + } +} \ No newline at end of file diff --git a/module/Application/src/Application/Controller/Plugin/MailWithLogPluginFactory.php b/module/Application/src/Application/Controller/Plugin/MailWithLogPluginFactory.php new file mode 100644 index 0000000000..4d6a7c9465 --- /dev/null +++ b/module/Application/src/Application/Controller/Plugin/MailWithLogPluginFactory.php @@ -0,0 +1,53 @@ +<?php + +namespace Application\Controller\Plugin; + +use UnicaenApp\Controller\Plugin\Mail; +use UnicaenApp\Options\ModuleOptions; +use Zend\Log\Logger; +use Zend\Log\Writer\Stream; +use Zend\Mail\Transport\Smtp; +use Zend\Mail\Transport\SmtpOptions; +use Zend\ServiceManager\Exception\InvalidArgumentException; +use Zend\ServiceManager\FactoryInterface; +use Zend\ServiceManager\ServiceLocatorInterface; + +/** + * Description of MailFactory + * + * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr> + */ +class MailWithLogPluginFactory implements FactoryInterface +{ + /** + * Create service + * + * @param ServiceLocatorInterface $pluginManager + * @return Mail + */ + public function createService(ServiceLocatorInterface $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."); + } + + $transport = new Smtp(new SmtpOptions($mailOptions['transport_options'])); + $plugin = new MailWithLogPlugin($transport); + + if (isset($mailOptions['redirect_to'])) { + $plugin->setRedirectTo($mailOptions['redirect_to']); + } + if (isset($mailOptions['do_not_send'])) { + $plugin->setDoNotSend($mailOptions['do_not_send']); + } + + $logger = new Logger(); + $logger->addWriter(new Stream(APPLICATION_PATH . "/data/mail.log")); + $plugin->setLogger($logger); + + return $plugin; + } +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index 0dc15dbf60..0f1f1808d9 100755 --- a/public/index.php +++ b/public/index.php @@ -11,6 +11,8 @@ if (! in_array($_SERVER['REMOTE_ADDR'],[ } /* Fin de fermeture du service*/ +define('APPLICATION_PATH', realpath(__DIR__ . "/..")); + define('REQUEST_MICROTIME', microtime(true)); /** * This makes our life easier when dealing with paths. Everything is relative -- GitLab