_controller = $collection->getController(); parent::__construct($collection, $settings); } /** * Initialize component * * @param Controller $controller Instantiating controller * @return void */ public function initialize(Controller $controller) { if (Configure::read('App.encoding') !== null) { $this->charset = Configure::read('App.encoding'); } } /** * Send an email using the specified content, template and layout * * @param string|array $content Either an array of text lines, or a string with contents * If you are rendering a template this variable will be sent to the templates as `$content` * @param string $template Template to use when sending email * @param string $layout Layout to use to enclose email body * @return boolean Success */ public function send($content = null, $template = null, $layout = null) { $lib = new CakeEmail(); $lib->charset = $this->charset; $lib->headerCharset = $this->charset; $lib->from($this->_formatAddresses((array)$this->from)); if (!empty($this->to)) { $lib->to($this->_formatAddresses((array)$this->to)); } if (!empty($this->cc)) { $lib->cc($this->_formatAddresses((array)$this->cc)); } if (!empty($this->bcc)) { $lib->bcc($this->_formatAddresses((array)$this->bcc)); } if (!empty($this->replyTo)) { $lib->replyTo($this->_formatAddresses((array)$this->replyTo)); } if (!empty($this->return)) { $lib->returnPath($this->_formatAddresses((array)$this->return)); } if (!empty($this->readReceipt)) { $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt)); } $lib->subject($this->subject)->messageID($this->messageId); $lib->helpers($this->_controller->helpers); $headers = array('X-Mailer' => $this->xMailer); foreach ($this->headers as $key => $value) { $headers['X-' . $key] = $value; } if ($this->date) { $headers['Date'] = $this->date; } $lib->setHeaders($headers); if ($template) { $this->template = $template; } if ($layout) { $this->layout = $layout; } $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs); if (!empty($this->attachments)) { $lib->attachments($this->_formatAttachFiles()); } $lib->transport(ucfirst($this->delivery)); if ($this->delivery === 'mail') { $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams)); } elseif ($this->delivery === 'smtp') { $lib->config($this->smtpOptions); } else { $lib->config(array()); } $sent = $lib->send($content); $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML); if (empty($this->htmlMessage)) { $this->htmlMessage = null; } $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT); if (empty($this->textMessage)) { $this->textMessage = null; } $this->_header = array(); $this->_message = array(); return $sent; } /** * Reset all EmailComponent internal variables to be able to send out a new email. * * @return void */ public function reset() { $this->template = null; $this->to = array(); $this->from = null; $this->replyTo = null; $this->return = null; $this->cc = array(); $this->bcc = array(); $this->subject = null; $this->additionalParams = null; $this->date = null; $this->attachments = array(); $this->htmlMessage = null; $this->textMessage = null; $this->messageId = true; $this->delivery = 'mail'; } /** * Format the attach array * * @return array */ protected function _formatAttachFiles() { $files = array(); foreach ($this->attachments as $filename => $attachment) { $file = $this->_findFiles($attachment); if (!empty($file)) { if (is_int($filename)) { $filename = basename($file); } $files[$filename] = $file; } } return $files; } /** * Find the specified attachment in the list of file paths * * @param string $attachment Attachment file name to find * @return string Path to located file */ protected function _findFiles($attachment) { if (file_exists($attachment)) { return $attachment; } foreach ($this->filePaths as $path) { if (file_exists($path . DS . $attachment)) { $file = $path . DS . $attachment; return $file; } } return null; } /** * Format addresses to be an array with email as key and alias as value * * @param array $addresses * @return array */ protected function _formatAddresses($addresses) { $formatted = array(); foreach ($addresses as $address) { if (preg_match('/((.*))?\s?<(.+)>/', $address, $matches) && !empty($matches[2])) { $formatted[$this->_strip($matches[3])] = $matches[2]; } else { $address = $this->_strip($address); $formatted[$address] = $address; } } return $formatted; } /** * Remove certain elements (such as bcc:, to:, %0a) from given value. * Helps prevent header injection / manipulation on user content. * * @param string $value Value to strip * @param boolean $message Set to true to indicate main message content * @return string Stripped value */ protected function _strip($value, $message = false) { $search = '%0a|%0d|Content-(?:Type|Transfer-Encoding)\:'; $search .= '|charset\=|mime-version\:|multipart/mixed|(?:[^a-z]to|b?cc)\:.*'; if ($message !== true) { $search .= '|\r|\n'; } $search = '#(?:' . $search . ')#i'; while (preg_match($search, $value)) { $value = preg_replace($search, '', $value); } return $value; } }