1. <?php
  2. namespace App\Controller\Component;
  3.  
  4. use Cake\Controller\Component;
  5. use Cake\Core\App;
  6. use Cake\Mailer\Email;
  7. use Cake\Mailer\Exception\MissingMailerException;
  8.  
  9. class MailerComponent extends Component
  10. {
  11. public function getMailer($name, Email $email = null)
  12. {
  13. if ($email === null) {
  14. $email = new Email();
  15. }
  16.  
  17. $className = App::className($name, 'Mailer', 'Mailer');
  18.  
  19. if (empty($className)) {
  20. throw new MissingMailerException(compact('name'));
  21. }
  22.  
  23. return (new $className($email));
  24. }
  25. }