Example #1
0
 /**
  * Make sending email available to models (as well as controllers)
  * Subject and message have duel meanigns if it starts with webpages. we are using the db to genetrate the message were the subject is the recored we are using
  *
  * @param string		String - address to send email to
  * @param sring			$subject: subject of email |OR|  Webpage to use as a template (eg. Webpages.name-of-template)
  * @param string		$message['html'] in the layout will be replaced with this text |OR| data array available to use for message replacement 
  * @param string		$template to be picked from folder for email. By default, if $mail is given in any template.
  * @param array			address/name pairs (e.g.: array(example@address.com => name, ...)
  * @param UNKNOWN		Have not used it don't know what it does or if it works.
  * @return bool
  */
 public function __sendMail($toEmail = null, $subject = null, $message = null, $template = 'default', $from = array(), $attachment = null)
 {
     if ($this->notifications !== false) {
         App::uses('AppController', 'Controller');
         $Controller = new AppController();
         if (is_array($subject)) {
             // a new and improved subject over the template, this one includes a fallback message if the template doesn't exist
             App::uses('Webpage', 'Webpages.Model');
             $Webpage = new Webpage();
             $name = str_replace('Webpages.', '', $subject['message']);
             $webpage = $Webpage->findByName($name);
             if (!empty($webpage)) {
                 // $message should be something like this : array('SomeModel' => array('some_field' => 'some_value'));
                 $message = $Webpage->replaceTokens($webpage['Webpage']['content'], $subject['data']);
                 $subject = $webpage['Webpage']['title'];
             } elseif (!empty($toEmail) && !empty($subject['subjectFallback']) && !empty($subject['messageFallback'])) {
                 // send the fallback
                 return $Controller->__sendMail($toEmail, $subject['subjectFallback'], $subject['messageFallback'], $template, $from, $attachment);
             } else {
                 throw new Exception(__('Please create an email template or fallback message.'));
             }
         } elseif (strpos($subject, 'Webpages.') === 0) {
             App::uses('Webpage', 'Webpages.Model');
             $Webpage = new Webpage();
             $name = str_replace('Webpages.', '', $subject);
             $webpage = $Webpage->findByName($name);
             if (!empty($webpage)) {
                 // $message should be something like this : array('SomeModel' => array('some_field' => 'some_value'));
                 $message = $Webpage->replaceTokens($webpage['Webpage']['content'], $message);
                 $subject = $webpage['Webpage']['title'];
             } else {
                 //Should we auto gen instead of throwing exception????
                 throw new Exception(__('Please create a email template named %s', $name));
             }
         }
         return $Controller->__sendMail($toEmail, $subject, $message, $template, $from, $attachment);
     }
     return true;
     // return true when notifications are off, so that checks don't fail
 }
Example #2
0
 /**
  * Make sending email available to models (as well as controllers)
  * Subject and message have duel meanigns if it starts with webpages. we are using the db to genetrate the message were the subject is the recored we are using
  *
  * @param string		String - address to send email to
  * @param sring			$subject: subject of email.
  * @param string		$message['html'] in the layout will be replaced with this text.
  * @param string		$template to be picked from folder for email. By default, if $mail is given in any template.
  * @param array			address/name pairs (e.g.: array(example@address.com => name, ...)
  * @param UNKNOWN		Have not used it don't know what it does or if it works.
  * @return bool
  */
 public function __sendMail($toEmail = null, $subject = null, $message = null, $template = 'default', $from = array(), $attachment = null)
 {
     if ($this->notifications !== false) {
         App::uses('AppController', 'Controller');
         $Controller = new AppController();
         if (strpos($subject, 'Webpages.') === 0) {
             $name = str_replace('Webpages.', '', $subject);
             App::uses('Webpage', 'Webpages.Model');
             $Webpage = new Webpage();
             $webpage = $Webpage->findByName($name);
             if (!empty($webpage)) {
                 // $message should be something like this : array('SomeModel' => array('some_field' => 'some_value'));
                 $message = $Webpage->replaceTokens($webpage['Webpage']['content'], $message);
                 $subject = $webpage['Webpage']['title'];
             } else {
                 //Should we auto gen instead of throwing exception????
                 throw new Exception(__('Please create a email template named %s', $name));
             }
         }
         return $Controller->__sendMail($toEmail, $subject, $message, $template, $from, $attachment);
     }
 }