Skip to content
Snippets Groups Projects
Commit 3529c7b0 authored by gauthierb's avatar gauthierb
Browse files

Envoi de mails : ajout de logs.

parent 399388d6
Branches
Tags
No related merge requests found
......@@ -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',
),
);
}
......
......@@ -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;
}
......
<?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
<?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
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment