Exemple #1
0
 /**
  * Wrapper around the email component, simplifying sending the kinds of emails we want to send.
  *
  * @param mixed $opts Array of options controlling the email.
  * @return mixed true if the email was sent, false otherwise.
  *
  */
 function _sendMail($opts)
 {
     App::import('Component', 'Email');
     $email = new EmailComponent();
     // Set up default values where applicable
     if (!array_key_exists('from', $opts)) {
         $opts['from'] = Configure::read('email.admin_name') . ' <' . Configure::read('email.admin_email') . '>';
     }
     // Set some details from the configuration
     if (Configure::read('email.use_smtp')) {
         $opts['delivery'] = 'smtp';
         $opts['smtpOptions'] = array_merge($email->smtpOptions, Configure::read('email.smtp_options'));
     }
     if (Configure::read('email.debug')) {
         $opts['delivery'] = 'debug';
     }
     // We may have been given complex Person arrays that the sender wants us to extract details from
     foreach (array('to' => true, 'cc' => true, 'bcc' => true, 'from' => false, 'replyTo' => false) as $key => $array) {
         if (array_key_exists($key, $opts)) {
             $opts[$key] = $this->_extractEmails($opts[$key], $array);
         }
     }
     // If there are no recipients, don't even bother trying to send
     if (empty($opts['to'])) {
         return array_key_exists('ignore_empty_address', $opts) && $opts['ignore_empty_address'];
     }
     // Add any custom headers
     if (array_key_exists('header', $opts)) {
         $email->header($opts['header']);
     }
     // Check if there are attachments to be included
     $email->attachments = Configure::read('email.attachments');
     if (empty($email->attachments)) {
         $email->attachments = array();
     }
     if (!empty($opts['attachments'])) {
         $email->attachments = array_merge($email->attachments, $opts['attachments']);
     }
     if (!empty($email->attachments)) {
         $email->filePaths = Configure::read('email.attachment_paths');
     }
     // Get ready and send it
     $email->initialize($this, $opts);
     if (array_key_exists('content', $opts)) {
         $success = $email->send($opts['content']);
     } else {
         $success = $email->send();
     }
     if (!empty($email->smtpError)) {
         $this->log("smtp-errors: {$email->smtpError}");
     }
     return $success;
 }