1

Is it possible to log emails sent using CakeEmail using the new (2.1+) Events system?

I'm hoping I can do something like this, only what I have doesn't do anything yet:

// config/bootstrap.php
App::uses('CakeEmailRecord', 'Lib/Event');
App::uses('CakeEventManager', 'Event');
CakeEventManager::instance()->attach(new CakeEmailRecord());


 // /app/Lib/Event/CakeEmailRecord.php
  App::uses('CakeEventListener', 'Event');
  class CakeEmailRecord implements CakeEventListener {
      public function implementedEvents() {
          return array(
              'Network.CakeEmail.afterSend' => 'recordSend',
          );
      }
      public function recordSend($event) {
         $this->log("triggered an event");
      }
  }

I'm specifically asking about the events system here. I want to know if that feature is something I can & should use to solve this. The question tagged as a duplicate does not deal with the cake events system

Will
  • 4,498
  • 2
  • 38
  • 65
  • 1
    possible duplicate of [CakePhp: Cake Email AfterSend event](http://stackoverflow.com/questions/30138083/cakephp-cake-email-aftersend-event) – ndm Jul 18 '15 at 17:51
  • @ndm - same outcome but asking about a different mechanism. Both of the solutions discussed there are fairly poor workarounds. Ideally the new events system would provide a better solution. – Will Jul 19 '15 at 13:10
  • @Will Well, it doesn't, so... what's the point? If you think that triggering events out of the box would be something the `CakeEmail` class could benefit from, then you could [**suggest that as an enhancement**](https://github.com/cakephp/cakephp/issues), other than that you have to use one of these "workarounds". – ndm Jul 19 '15 at 13:35
  • @ndm, it doesn't? cakeEmail doesn't fire any events at all? – Will Jul 19 '15 at 14:30
  • No, it doesn't, that's the point of the linked question. Currently you'd have to implement that on your own in an extended email class. – ndm Jul 19 '15 at 14:58
  • ok, so the linked question does not have any good answers. option 1 is pretty vague / not sure it allows access to things like subject, to, from, message for db logging and option 2 involves changing the existing code everywhere to use the new class / a function to use a new class. – Will Jul 19 '15 at 15:06

0 Answers0