Posted By

ryantxr on 11/13/12


Tagged


Versions (?)

CakePhp email notification component


 / Published in: PHP
 

This is a CakePHP component I used for sending out email notifications.

  1. <?php
  2. /**
  3.  * This component handles sending email to webmaster
  4.  */
  5. App::uses('CakeEmail', 'Network/Email');
  6. class NotifyComponent extends Component{
  7.  
  8. function notify($msg, $subject=null, $to=null, $smtp=false){
  9. if ( ! $to ) $to = '[email protected]';
  10. $t = strftime('%Y-%m-%d %T ');
  11. $body = "Website Notice.\n" . $t . "\n" . $msg;
  12. $notifyEmail = Configure::read('notify-email');
  13. $webmasterEmail = Configure::read('webmaster-email');
  14. $webmasterName = Configure::read('webmaster-name');
  15. if ( $subject == null ) $subject = $webmasterName . ' - Site Notice';
  16. $email = $this->createEmailObject($smtp);
  17.  
  18. $email->from(array($webmasterEmail => $webmasterName))
  19. ->to($to)
  20. ->subject($subject);
  21. $email->sender($webmasterEmail, $webmasterName);
  22. $email->send($body);
  23. }
  24.  
  25. function critical($msg, $trace=null){
  26.  
  27. $t = strftime('%Y-%m-%d %T ');
  28. $body = "Critical error.\n" . $t . "\n" . $msg;
  29. if ( $trace ){
  30. $body .="\n\n";
  31. if ( is_array($trace) ){
  32. $body .= print_r($trace, true);
  33. }
  34. else if( is_object($trace) ){
  35. $body .= print_r($trace,true);
  36. }
  37. else{
  38. $body .= $trace;
  39. }
  40. }
  41. $notifyEmail = Configure::read('notify-email');
  42. $webmasterEmail = Configure::read('webmaster-email');
  43. $webmasterName = Configure::read('webmaster-name');
  44. $email = $this->createEmailObject($smtp);
  45.  
  46. $email->from(array($webmasterEmail => $webmasterName))
  47. ->to($notifyEmail)
  48. ->subject($webmasterName.' - Critical site error');
  49. $email->sender($webmasterEmail, $webmasterName);
  50. $email->send($body);
  51.  
  52. }
  53.  
  54. function createEmailObject($smtp=false){
  55. $email = new CakeEmail();
  56.  
  57. $emailConfig = Configure::read('email-config');
  58. // force smtp if the parameter says so even if it is turned off in the config.
  59. if ( $smtp ){
  60. $email->config('smtp');
  61. }
  62. if ( ! empty($emailConfig) ) {
  63. $email->config($emailConfig);
  64. }
  65. return $email;
  66. }
  67.  
  68. }
  69. ?>

Report this snippet  

You need to login to post a comment.