charset = $charset; } } /** * Set From * * @param mixed $email * @param string $name * @return void * @thrown SocketException */ public function setFrom($email, $name = null) { $this->_setEmail1('_from', $email, $name, __('From requires only 1 email address.')); } /** * Get the From information * * @return array Key is email, Value is name. If Key is equal of Value, the name is not specified */ public function getFrom() { return $this->_from; } /** * Set Reply-To * * @param mixed $email * @param string $name * @return void * @thrown SocketException */ public function setReplyTo($email, $name = null) { $this->_setEmail1('_replyTo', $email, $name, __('Reply-To requires only 1 email address.')); } /** * Get the ReplyTo information * * @return array Key is email, Value is name. If Key is equal of Value, the name is not specified */ public function getReplyTo() { return $this->_replyTo; } /** * Set Read Receipt (Disposition-Notification-To header) * * @param mixed $email * @param string $name * @return void * @thrown SocketException */ public function setReadReceipt($email, $name = null) { $this->_setEmail1('_readReceipt', $email, $name, __('Disposition-Notification-To requires only 1 email address.')); } /** * Get the Read Receipt (Disposition-Notification-To header) information * * @return array Key is email, Value is name. If Key is equal of Value, the name is not specified */ public function getReadReceipt() { return $this->_readReceipt; } /** * Set Return Path * * @param mixed $email * @param string $name * @return void * @thrown SocketException */ public function setReturnPath($email, $name = null) { $this->_setEmail1('_returnPath', $email, $name, __('Return-Path requires only 1 email address.')); } /** * Get the Return Path information * * @return array Key is email, Value is name. If Key is equal of Value, the name is not specified */ public function getReturnPath() { return $this->_returnPath; } /** * Set To * * @param mixed $email String with email, Array with email as key, name as value or email as value (without name) * @param string $name * @return void */ public function setTo($email, $name = null) { $this->_setEmail('_to', $email, $name); } /** * Add To * * @param mixed $email String with email, Array with email as key, name as value or email as value (without name) * @param string $name * @return void */ public function addTo($email, $name = null) { $this->_addEmail('_to', $email, $name); } /** * Get To * * @return array */ public function getTo() { return $this->_to; } /** * Set Cc * * @param mixed $email String with email, Array with email as key, name as value or email as value (without name) * @param string $name * @return void */ public function setCc($email, $name = null) { $this->_setEmail('_cc', $email, $name); } /** * Add Cc * * @param mixed $email String with email, Array with email as key, name as value or email as value (without name) * @param string $name * @return void */ public function addCc($email, $name = null) { $this->_addEmail('_cc', $email, $name); } /** * Get Cc * * @return array */ public function getCc() { return $this->_cc; } /** * Set Bcc * * @param mixed $email String with email, Array with email as key, name as value or email as value (without name) * @param string $name * @return void */ public function setBcc($email, $name = null) { $this->_setEmail('_bcc', $email, $name); } /** * Add Bcc * * @param mixed $email String with email, Array with email as key, name as value or email as value (without name) * @param string $name * @return void */ public function addBcc($email, $name = null) { $this->_addEmail('_bcc', $email, $name); } /** * Get Bcc * * @return array */ public function getBcc() { return $this->_bcc; } /** * Set email * * @param string $varName * @param mixed $email * @param mixed $name * @return void * @thrown SocketException */ protected function _setEmail($varName, $email, $name) { if (!is_array($email)) { if (!Validation::email($email)) { throw new SocketException(__('Invalid email: "%s"', $email)); } if ($name === null) { $name = $email; } $this->{$varName} = array($email => $name); return; } $list = array(); foreach ($email as $key => $value) { if (is_int($key)) { $key = $value; } if (!Validation::email($key)) { throw new SocketException(__('Invalid email: "%s"', $key)); } $list[$key] = $value; } $this->{$varName} = $list; } /** * Set only 1 email * * @param string $varName * @param mixed $email * @param string $name * @param string $throwMessage * @return void * @thrown SocketExpceiton */ protected function _setEmail1($varName, $email, $name, $throwMessage) { $current = $this->{$varName}; $this->_setEmail($varName, $email, $name); if (count($this->{$varName}) !== 1) { $this->{$varName} = $current; throw new SocketException($throwMessage); } } /** * Add email * * @param string $varName * @param mixed $email * @param mixed $name * @return void */ protected function _addEmail($varName, $email, $name) { if (!is_array($email)) { if (!Validation::email($email)) { throw new SocketException(__('Invalid email: "%s"', $email)); } if ($name === null) { $name = $email; } $this->{$varName}[$email] = $name; return; } $list = array(); foreach ($email as $key => $value) { if (is_int($key)) { $key = $value; } if (!Validation::email($key)) { throw new SocketException(__('Invalid email: "%s"', $key)); } $list[$key] = $value; } $this->{$varName} = array_merge($this->{$varName}, $list); } /** * Set Subject * * @param string $subject * @return void */ public function setSubject($subject) { $this->_subject = (string)$subject; } /** * Get Subject * * @return string */ public function getSubject() { return $this->_subject; } /** * Sets headers for the message * * @param array Associative array containing headers to be set. * @return void * @thrown SocketException */ public function setHeaders($headers) { if (!is_array($headers)) { throw new SocketException(__('$headers should be an array.')); } $this->_headers = $headers; } /** * Add header for the message * * @param array $headers * @return void * @thrown SocketException */ public function addHeaders($headers) { if (!is_array($headers)) { throw new SocketException(__('$headers should be an array.')); } $this->_headers = array_merge($this->_headers, $headers); } /** * Get list of headers * * ### Includes: * * - `from` * - `replyTo` * - `readReceipt` * - `returnPath` * - `to` * - `cc` * - `bcc` * - `subject` * * @param array $include * @return array */ public function getHeaders($include = array()) { $defaults = array( 'from' => false, 'replyTo' => false, 'readReceipt' => false, 'returnPath' => false, 'to' => false, 'cc' => false, 'bcc' => false, 'subject' => false ); $include += $defaults; $headers = array(); $relation = array( 'from' => 'From', 'replyTo' => 'Reply-To', 'readReceipt' => 'Disposition-Notification-To', 'returnPath' => 'Return-Path' ); foreach ($relation as $var => $header) { if ($include[$var]) { $var = '_' . $var; $headers[$header] = current($this->_formatAddress($this->{$var})); } } foreach (array('to', 'cc', 'bcc') as $var) { if ($include[$var]) { $classVar = '_' . $var; $headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar})); } } $headers += $this->_headers; if (!isset($headers['X-Mailer'])) { $headers['X-Mailer'] = Configure::read('Email.XMailer'); if (empty($headers['X-Mailer'])) { $headers['X-Mailer'] = self::EMAIL_CLIENT; } } if (!isset($headers['Date'])) { $headers['Date'] = date(DATE_RFC2822); } if ($this->_messageId !== false) { if ($this->_messageId === true) { $headers['Message-ID'] = '<' . String::UUID() . '@' . env('HTTP_HOST') . '>'; } else { $headers['Message-ID'] = $this->_messageId; } } if ($include['subject']) { $headers['Subject'] = $this->_subject; } if (!empty($this->_attachments)) { $this->_createBoundary(); $headers['MIME-Version'] = '1.0'; $headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"'; $headers[] = 'This part of the E-mail should never be seen. If'; $headers[] = 'you are reading this, consider upgrading your e-mail'; $headers[] = 'client to a MIME-compatible client.'; } elseif ($this->_emailFormat === 'text') { $headers['Content-Type'] = 'text/plain; charset=' . $this->charset; } elseif ($this->_emailFormat === 'html') { $headers['Content-Type'] = 'text/html; charset=' . $this->charset; } elseif ($this->_emailFormat === 'both') { $headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"'; } $headers['Content-Transfer-Encoding'] = '7bit'; return $headers; } /** * Format addresses * * @param array $address * @return array */ protected function _formatAddress($address) { $return = array(); foreach ($address as $email => $alias) { if ($email === $alias) { $return[] = $email; } else { $return[] = sprintf('%s <%s>', $this->_encode($alias), $email); } } return $return; } /** * Set the layout and template * * @param string $layout * @param string $template * @return void */ public function setLayout($layout, $template = null) { $this->_layout = (string)$layout; if ($template !== null) { $this->_template = (string)$template; } } /** * Set view class for render * * @param string $viewClass * @return void */ public function setViewRender($viewClass) { $this->_viewRender = $viewClass; } /** * Set the email format * * @param string $format * @return void * @thrown SocketException */ public function setEmailFormat($format) { if (!in_array($format, $this->_emailFormatAvailable)) { throw new SocketException(__('Format not available.')); } $this->_emailFormat = $format; } /** * Set transport name * * @param string $name * @return void */ public function setTransport($name) { $this->_transportName = (string)$name; } /** * Set Message-ID * * @param mixed $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID * @return void * @thrown SocketException */ public function setMessageID($message) { if (is_bool($message)) { $this->_messageId = $message; } else { if (!preg_match('/^\<.+@.+\>$/', $message)) { throw new SocketException(__('Invalid format to Message-ID. The text should be something like ""')); } $this->_messageId = $message; } } /** * Set attachments * * @param mixed $attachments String with the filename or array with filenames * @return void * @thrown SocketException */ public function setAttachments($attachments) { $attach = array(); foreach ((array)$attachments as $name => $file) { $path = realpath($file); if ($path === false) { throw new SocketException(__('File not found: "%s"', $attach)); } if (is_int($name)) { $name = basename($path); } $attach[$name] = $path; } $this->_attachments = $attach; } /** * Add attachments * * @param mixed $attachments String with the filename or array with filenames * @return void * @thrown SocketException */ public function addAttachments($attachments) { $current = $this->_attachments; $this->setAttachments($attachments); $this->_attachments = array_merge($current, $this->_attachments); } /** * Get attachments * * @return array */ public function getAttachments() { return $this->_attachments; } /** * Get generated message (used by transport classes) * * @return array */ public function getMessage() { return $this->_message; } /** * Send an email using the specified content, template and layout * * @return boolean Success * @thrown SocketExpcetion */ public function send($content = null) { if (empty($this->_from)) { throw new SocketException(__('From is not specified.')); } if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) { throw new SocketExpcetion(__('You need specify one destination on to, cc or bcc.')); } if (is_array($content)) { $content = implode("\n", $content) . "\n"; } $this->_textMessage = $this->_htmlMessage = ''; if ($content !== null) { if ($this->_emailFormat === 'text') { $this->_textMessage = $content; } elseif ($this->_emailFormat === 'html') { $this->_htmlMessage = $content; } elseif ($this->_emailFormat === 'both') { $this->_textMessage = $this->_htmlMessage = $content; } } $message = $this->_wrap($content); if (empty($this->_template)) { $message = $this->_formatMessage($message); } else { $message = $this->_render($message); } $message[] = ''; $this->_message = $message; if (!empty($this->_attachments)) { $this->_attachFiles(); } if (!is_null($this->_boundary)) { $this->_message[] = ''; $this->_message[] = '--' . $this->_boundary . '--'; $this->_message[] = ''; } $transportClassname = Inflector::camelize($this->_transportName) . 'Transport'; if (!App::import('Lib', 'email/' . $transportClassname)) { throw new SocketException(__('Class "%s" not found.', $transportClassname)); } elseif (!method_exists($transportClassname, 'send')) { throw new SocketException(__('The "%s" do not have send method.', $transportClassname)); } $transport = new $transportClassname(); return $transport->send($this); } /** * Reset all EmailComponent internal variables to be able to send out a new email. * * @return void */ public function reset() { $this->_to = array(); $this->_from = array(); $this->_replyTo = array(); $this->_readReceipt = array(); $this->_returnPath = array(); $this->_cc = array(); $this->_bcc = array(); $this->_messageId = true; $this->_subject = ''; $this->_headers = array(); $this->_layout = 'default'; $this->_template = ''; $this->_viewRender = 'View'; $this->_textMessage = ''; $this->_htmlMessage = ''; $this->_message = ''; $this->_emailFormat = 'text'; $this->_transportName = 'mail'; $this->_attachments = array(); } /** * Encode the specified string using the current charset * * @param string $text String to encode * @return string Encoded string */ protected function _encode($text) { $internalEncoding = function_exists('mb_internal_encoding'); if ($internalEncoding) { $restore = mb_internal_encoding(); mb_internal_encoding($this->charset); } $return = mb_encode_mimeheader($text, $this->charset, 'B'); if ($internalEncoding) { mb_internal_encoding($restore); } return $return; } /** * Wrap the message to follow the RFC 2822 - 2.1.1 * * @param string $message Message to wrap * @return array Wrapped message * @access protected */ function _wrap($message) { $message = str_replace(array("\r\n", "\r"), "\n", $message); $lines = explode("\n", $message); $formatted = array(); foreach ($lines as $line) { if (empty($line)) { $formatted[] = ''; continue; } if ($line[0] === '.') { $line = '.' . $line; } if (!preg_match('/\<[a-z]/i', $line)) { $formatted = array_merge($formatted, explode("\n", wordwrap($line, self::LINE_LENGTH_SHOULD, "\n", true))); continue; } $tagOpen = false; $tmpLine = $tag = ''; $tmpLineLength = 0; for ($i = 0, $count = strlen($line); $i < $count; $i++) { $char = $line[$i]; if ($tagOpen) { $tag .= $char; if ($char === '>') { $tagLength = strlen($tag); if ($tagLength + $tmpLineLength < self::LINE_LENGTH_SHOULD) { $tmpLine .= $tag; $tmpLineLength += $tagLength; } else { if ($tmpLineLength > 0) { $formatted[] = trim($tmpLine); $tmpLine = ''; $tmpLineLength = 0; } if ($tagLength > self::LINE_LENGTH_SHOULD) { $formatted[] = $tag; } else { $tmpLine = $tag; $tmpLineLength = $tagLength; } } $tag = ''; $tagOpen = false; } continue; } if ($char === '<') { $tagOpen = true; $tag = '<'; continue; } if ($char === ' ' && $tmpLineLength >= self::LINE_LENGTH_SHOULD) { $formatted[] = $tmpLine; $tmpLineLength = 0; continue; } $tmpLine .= $char; $tmpLineLength++; if ($tmpLineLength === self::LINE_LENGTH_SHOULD) { $nextChar = $line[$i + 1]; if ($nextChar === ' ' || $nextChar === '<') { $formatted[] = trim($tmpLine); $tmpLine = ''; $tmpLineLength = 0; if ($nextChar === ' ') { $i++; } } else { $lastSpace = strrpos($tmpLine, ' '); if ($lastSpace === false) { continue; } $formatted[] = trim(substr($tmpLine, 0, $lastSpace)); $tmpLine = substr($tmpLine, $lastSpace + 1); $tmpLineLength = strlen($tmpLine); } } } if (!empty($tmpLine)) { $formatted[] = $tmpLine; } } $formatted[] = ''; return $formatted; } /** * Create unique boundary identifier * * @return void */ function _createboundary() { $this->_boundary = md5(uniqid(time())); } /** * Attach files by adding file contents inside boundaries. * * @return void */ function _attachFiles() { foreach ($this->_attachments as $filename => $file) { $handle = fopen($file, 'rb'); $data = fread($handle, filesize($file)); $data = chunk_split(base64_encode($data)) ; fclose($handle); $this->_message[] = '--' . $this->_boundary; $this->_message[] = 'Content-Type: application/octet-stream'; $this->_message[] = 'Content-Transfer-Encoding: base64'; $this->_message[] = 'Content-Disposition: attachment; filename="' . $filename . '"'; $this->_message[] = ''; $this->_message[] = $data; $this->_message[] = ''; } } /** * Format the message by seeing if it has attachments. * * @param array $message Message to format * @return array */ function _formatMessage($message) { if (!empty($this->_attachments)) { $prefix = array('--' . $this->_boundary); if ($this->_emailFormat === 'text') { $prefix[] = 'Content-Type: text/plain; charset=' . $this->charset; } elseif ($this->_emailFormat === 'html') { $prefix[] = 'Content-Type: text/html; charset=' . $this->charset; } elseif ($this->_emailFormat === 'both') { $prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"'; } $prefix[] = 'Content-Transfer-Encoding: 7bit'; $prefix[] = ''; $message = array_merge($prefix, $message); } return $message; } /** * Render the contents using the current layout and template. * * @param string $content Content to render * @return array Email ready to be sent * @access private */ function _render($content) { $viewClass = $this->_viewRender; if ($viewClass !== 'View') { list($plugin, $viewClass) = pluginSplit($viewClass); $viewClass = $viewClass . 'View'; App::import('View', $this->_viewRender); } $View = new $viewClass(null); $View->layout = $this->_layout; $msg = array(); $content = implode("\n", $content); if ($this->_emailFormat === 'both') { $htmlContent = $content; if (!empty($this->_attachments)) { $msg[] = '--' . $this->_boundary; $msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"'; $msg[] = ''; } $msg[] = '--alt-' . $this->_boundary; $msg[] = 'Content-Type: text/plain; charset=' . $this->charset; $msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = ''; $content = $View->element('email' . DS . 'text' . DS . $this->_template, array('content' => $content), true); $View->layoutPath = 'email' . DS . 'text'; $content = explode("\n", $this->_textMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content))); $msg = array_merge($msg, $content); $msg[] = ''; $msg[] = '--alt-' . $this->_boundary; $msg[] = 'Content-Type: text/html; charset=' . $this->charset; $msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = ''; $htmlContent = $View->element('email' . DS . 'html' . DS . $this->_template, array('content' => $htmlContent), true); $View->layoutPath = 'email' . DS . 'html'; $htmlContent = explode("\n", $this->_htmlMessage = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent))); $msg = array_merge($msg, $htmlContent); $msg[] = ''; $msg[] = '--alt-' . $this->_boundary . '--'; $msg[] = ''; ClassRegistry::removeObject('view'); return $msg; } if (!empty($this->_attachments)) { if ($this->_emailFormat === 'html') { $msg[] = ''; $msg[] = '--' . $this->_boundary; $msg[] = 'Content-Type: text/html; charset=' . $this->charset; $msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = ''; } else { $msg[] = '--' . $this->_boundary; $msg[] = 'Content-Type: text/plain; charset=' . $this->charset; $msg[] = 'Content-Transfer-Encoding: 7bit'; $msg[] = ''; } } $content = $View->element('email' . DS . $this->_emailFormat . DS . $this->_template, array('content' => $content), true); $View->layoutPath = 'email' . DS . $this->_emailFormat; $content = explode("\n", $rendered = str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content))); if ($this->_emailFormat === 'html') { $this->_htmlMessage = $rendered; } else { $this->_textMessage = $rendered; } $msg = array_merge($msg, $content); ClassRegistry::removeObject('view'); return $msg; } }