We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Trying to use incubator Phalcon\Mailer and eventsManager

Hello, I'm working on my first big project with Phalcon and I want to use Phaclon\Mailer from incubator https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mailer and attach an event to mailer:beforeSend but it's never reached.

In my public/index.php set eventsManager :

$di = new FactoryDefault();

$di->setShared('eventsManager', function(){
$eventsManager = new EventsManager();

$eventsManager->attach('mailer:beforeSend', function($mailer) {
var_dump($mailer);
die('here');
});

return $eventsManager;
});

etc...

In Phalcon\Mailer\Message.php the send() function get eventsManager with :

$eventManager = $this->getManager()->getEventsManager();

var_dump($eventManger) : /app/vendor/phalcon/incubator/Library/Phalcon/Mailer/Message.php:634:null

But if I add ->getDi() : $eventManager = $this->getManager()->getDi()->getEventsManager(); my defined eventsManager is good.

I d'ont understand if my eventsManager is wrong registered or something else.

Thanks

Probably isn't the same event manager check with spl_object_hash()

Good luck



979
edited Nov '18

Thanks, I'll check tomorrow.

I've searched wich events manager it's using, but dit not find.



979

**Seems bad, it's null

// Phalcon\Mailer\Manager.php:86
public function createMessage()
{
var_dump($this->getEventsManager());
spl_object_hash($this->getEventsManager());
die;

Return : /app/vendor/phalcon/incubator/Library/Phalcon/Mailer/Manager.php:88:null

( ! ) Warning: spl_object_hash() expects parameter 1 to be object, null given in /app/vendor/phalcon/incubator/Library/Phalcon/Mailer/Manager.php on line 89**

I tried to add : $di->setInternalEventsManager($di->getShared('eventsManager')); but same result in Phalcon\Mailer\Manager.php

So seeing that :

  • 1) Manager.php extends Phalcon\Mvc\User\Component
  • 2) and Phalcon\Mvc\User\Component extends Phalcon\Di\Injectable
  • 3) Phalcon\Di\Injectable as a setEventsManager()

I added

$mailer  = new MailerManager($this->di->getConfig()['mailer']->toArray());
$mailer->setEventsManager($this->di->getEventsManager()); // New line set the eventsManager for component

I'll put my MailerManager in the DI.

Thats working, but don't really know if it's the best solution....

I would use the first option $eventManager = $this->getManager()->getEventsManager();



979

Sorry, I'm replying very late.
Thanks for your reply.