您必须首先确定“Mailer
发送电子邮件失败”的含义。如果您可以在课堂上检查这种情况,Mailer
则必须触发相应mail.error
或类似的事件。
然后你必须在EventManager
内部附加一个监听器来Mailer
监听这个mail.error
事件并记录错误。
内触发错误Mailer
假设我们的Mailer
类看起来像这样:
<?php
namespace Project\Mail;
class Mailer
{
const EVENT_MAIL_ERROR = 'mail.error';
protected $events;
public function setEventManager(EventManagerInterface $events)
{
$this->events = $events;
return $this;
}
public function getEventManager()
{
if ($this->events === null)
{
$this->setEventManager(new EventManager);
}
return $this->events;
}
public function send(MessageInterface $msg)
{
// try sending the message. uh-oh we failed!
if ($someErrorCondition)
{
$this->getEventManager()->trigger(self::EVENT_MAIL_ERROR, $this, array(
'custom-param' => 'failure reason',
));
}
}
}
监听事件
在引导期间,我们将侦听器附加到EventManager
inside Mailer
。
<?php
namespace FooBar;
use Zend\EventManager\Event;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$services = $application->getServiceManager();
$mailer = $services->get('Mailer');
$mailer->getEventManager()->attach(Mailer::EVENT_MAIL_ERROR, function(Event $event)
{
$param = $event->getParam('custom-param');
// log the error
});
}
}
请参阅EventManager上的文档了解实现细节。
我希望这能解决你的问题!