Good morning everybody,
I\'m currently using CakePHP.
I would like to setup an event after an email is sent by CakePHP, because I would like to store a log of
Implement your own logger with the DB features you want and configure it in bootstrap for the email scope which is the default scope for email logs or change the whole logging in the config of the email class. See this part of the email class.
1161: $contents = $this->transportClass()->send($this);
1162: if (!empty($this->_config['log'])) {
1163: $config = array(
1164: 'level' => LOG_DEBUG,
1165: 'scope' => 'email'
1166: );
1167: if ($this->_config['log'] !== true) {
1168: if (!is_array($this->_config['log'])) {
1169: $this->_config['log'] = array('level' => $this->_config['log']);
1170: }
1171: $config = $this->_config['log'] + $config;
1172: }
1173: CakeLog::write(
1174: $config['level'],
1175: PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message'],
1176: $config['scope']
1177: );
1178: }
Of course I can create a MyCakeEmail class that extends the native CakeEmail class, but this means changing every new CakeEmail() yet in the code.
Well you can use your own email class. But doing new SomeClass()
everywhere in the code isn't a good hing in any case IMHO. You just figured out why. Another reason to not do this ease of testing.
Instead do this in some class in the upper level of the inheritage chain (AppController, AppModel...):
public function getEmailInstance($config = null) {
return new MyEmailClass($config);
}
This allows you to simply change the class "globally" and to mock the method in tests as well.
If you're using php 5.4 (or 5.5, not sure right now) you can use a trait for that as well and use it only in classes that need that functionality.