📄 email.php
字号:
$content = $View->element('email' . DS . 'html' . DS . $this->template, array('content' => $htmlContent), true); $View->layoutPath = 'email' . DS . 'html'; $msg .= $View->renderLayout($content) . $this->_newLine . $this->_newLine; $msg .= '--alt-' . $this->__boundary . '--' . $this->_newLine . $this->_newLine; return $msg; } if (!empty($this->attachments)) { if ($this->sendAs === 'html') { $msg .= $this->_newLine. '--' . $this->__boundary . $this->_newLine; $msg .= 'Content-Type: text/html; charset=' . $this->charset . $this->_newLine; $msg .= 'Content-Transfer-Encoding: 7bit' . $this->_newLine . $this->_newLine; } else { $msg .= '--' . $this->__boundary . $this->_newLine; $msg .= 'Content-Type: text/plain; charset=' . $this->charset . $this->_newLine; $msg .= 'Content-Transfer-Encoding: 7bit' . $this->_newLine . $this->_newLine; } } $content = $View->element('email' . DS . $this->sendAs . DS . $this->template, array('content' => $content), true); $View->layoutPath = 'email' . DS . $this->sendAs; $msg .= $View->renderLayout($content) . $this->_newLine; return $msg; }/** * Create unique boundary identifier * * @access private */ function __createBoundary() { $this->__boundary = md5(uniqid(time())); }/** * Create emails headers including (but not limited to) from email address, reply to, * bcc and cc. * * @access private */ function __createHeader() { $this->__header = ''; if ($this->delivery == 'smtp') { $this->__header = 'To: ' . $this->__formatAddress($this->to) . $this->_newLine; } $this->__header .= 'From: ' . $this->__formatAddress($this->from) . $this->_newLine; if (!empty($this->replyTo)) { $this->__header .= 'Reply-To: ' . $this->__formatAddress($this->replyTo) . $this->_newLine; } if (!empty($this->return)) { $this->__header .= 'Return-Path: ' . $this->__formatAddress($this->return) . $this->_newLine; } if (!empty($this->readReceipt)) { $this->__header .= 'Disposition-Notification-To: ' . $this->__formatAddress($this->readReceipt) . $this->_newLine; } $addresses = null; if (!empty($this->cc)) { foreach ($this->cc as $cc) { $addresses .= ', ' . $this->__formatAddress($cc); } $this->__header .= 'cc: ' . substr($addresses, 2) . $this->_newLine; } $addresses = null; if (!empty($this->bcc)) { foreach ($this->bcc as $bcc) { $addresses .= ', ' . $this->__formatAddress($bcc); } $this->__header .= 'Bcc: ' . substr($addresses, 2) . $this->_newLine; } if ($this->delivery == 'smtp') { $this->__header .= 'Subject: ' . $this->__encode($this->subject) . $this->_newLine; } $this->__header .= 'X-Mailer: ' . $this->xMailer . $this->_newLine; if (!empty($this->headers)) { foreach ($this->headers as $key => $val) { $this->__header .= 'X-'.$key.': '.$val . $this->_newLine; } } if (!empty($this->attachments)) { $this->__createBoundary(); $this->__header .= 'MIME-Version: 1.0' . $this->_newLine; $this->__header .= 'Content-Type: multipart/mixed; boundary="' . $this->__boundary . '"' . $this->_newLine; $this->__header .= 'This part of the E-mail should never be seen. If' . $this->_newLine; $this->__header .= 'you are reading this, consider upgrading your e-mail' . $this->_newLine; $this->__header .= 'client to a MIME-compatible client.' . $this->_newLine; } elseif ($this->sendAs === 'text') { $this->__header .= 'Content-Type: text/plain; charset=' . $this->charset . $this->_newLine; } elseif ($this->sendAs === 'html') { $this->__header .= 'Content-Type: text/html; charset=' . $this->charset . $this->_newLine; } elseif ($this->sendAs === 'both') { $this->__header .= 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"' . $this->_newLine . $this->_newLine; } $this->__header .= 'Content-Transfer-Encoding: 7bit'; }/** * Format the message by seeing if it has attachments. * * @param string $message Message to format * @access private */ function __formatMessage($message) { $this->__message = ''; if (!empty($this->attachments)) { $this->__message .= '--' . $this->__boundary . $this->_newLine; $this->__message .= 'Content-Type: text/plain; charset=' . $this->charset . $this->_newLine; $this->__message .= 'Content-Transfer-Encoding: 7bit' . $this->_newLine . $this->_newLine; } $message = $this->__wrap($message); $this->__message .= $message . $this->_newLine; }/** * Attach files by adding file contents inside boundaries. * * @access private */ function __attachFiles() { $files = array(); foreach ($this->attachments as $attachment) { $file = $this->__findFiles($attachment); if (!empty($file)) { $files[] = $file; } } foreach ($files as $file) { $handle = fopen($file, 'rb'); $data = fread($handle, filesize($file)); $data = chunk_split(base64_encode($data)) ; fclose($handle); $this->__message .= '--' . $this->__boundary . $this->_newLine; $this->__message .= 'Content-Type: application/octet-stream' . $this->_newLine; $this->__message .= 'Content-Transfer-Encoding: base64' . $this->_newLine; $this->__message .= 'Content-Disposition: attachment; filename="' . basename($file) . '"' . $this->_newLine . $this->_newLine; $this->__message .= $data . $this->_newLine . $this->_newLine; } }/** * Find the specified attachment in the list of file paths * * @param string $attachment Attachment file name to find * @return string Path to located file * @access private */ 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; }/** * Wrap the message using EmailComponent::$lineLength * * @param string $message Message to wrap * @return string Wrapped message * @access private */ function __wrap($message) { $message = $this->__strip($message, true); $message = str_replace(array("\r\n","\r"), "\n", $message); $lines = explode("\n", $message); $formatted = null; if ($this->_lineLength !== null) { trigger_error('_lineLength cannot be accessed please use lineLength', E_USER_WARNING); $this->lineLength = $this->_lineLength; } foreach ($lines as $line) { if(substr($line, 0, 1) == '.') { $line = '.' . $line; } $formatted .= wordwrap($line, $this->lineLength, $this->_newLine, true); $formatted .= $this->_newLine; } return $formatted; }/** * Encode the specified string using the current charset * * @param string $subject String to encode * @return string Encoded string * @access private */ function __encode($subject) { $subject = $this->__strip($subject); if (low($this->charset) !== 'iso-8859-15') { $start = "=?" . $this->charset . "?B?"; $end = "?="; $spacer = $end . "\n " . $start; $length = 75 - strlen($start) - strlen($end); $length = $length - ($length % 4); $subject = base64_encode($subject); $subject = chunk_split($subject, $length, $spacer); $spacer = preg_quote($spacer); $subject = preg_replace("/" . $spacer . "$/", "", $subject); $subject = $start . $subject . $end; } return $subject; }/** * Format a string as an email address * * @param string $string String representing an email address * @return string Email address suitable for email headers or smtp pipe * @access private */ function __formatAddress($string, $smtp = false) { if (strpos($string, '<') !== false) { $value = explode('<', $string); if ($smtp) { $string = '<' . $value[1]; } else { $string = $this->__encode($value[0]) . ' <' . $value[1]; } } return $this->__strip($string); }/** * Remove certain elements (such as bcc:, to:, %0a) from given value * * @param string $value Value to strip * @param boolean $message Set to true to indicate main message content * @return string Stripped value * @access private */ function __strip($value, $message = false) { $search = array( '/%0a/i', '/%0d/i', '/Content-Type\:/i', '/charset\=/i', '/mime-version\:/i', '/multipart\/mixed/i', '/bcc\:.*/i','/to\:.*/i','/cc\:.*/i', '/Content-Transfer-Encoding\:/i', '/\\r/i', '/\\n/i' ); if ($message === true) { $search = array_slice($search, 0, -2); } foreach ($search as $key) { while (preg_match($key, $value)) { $value = preg_replace($key, '', $value); } } return preg_replace($search, '', $value); }/** * Wrapper for PHP mail function used for sending out emails * * @return bool Success * @access private */ function __mail() { if (ini_get('safe_mode')) { return @mail($this->to, $this->__encode($this->subject), $this->__message, $this->__header); } return @mail($this->to, $this->__encode($this->subject), $this->__message, $this->__header, $this->additionalParams); }/** * Sends out email via SMTP * * @return bool Success * @access private */ function __smtp() { App::import('Core', array('Socket')); $this->__smtpConnection =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->smtpOptions)); if (!$this->__smtpConnection->connect()) { $this->smtpError = $this->__smtpConnection->lastError(); return false; } elseif (!$this->__smtpSend(null, '220')) { return false; } if (!$this->__smtpSend('HELO cake', '250')) { return false; } if (isset($this->smtpOptions['username']) && isset($this->smtpOptions['password'])) { if (!$this->__smtpSend('AUTH LOGIN', '334')) { return false; } if (!$this->__smtpSend(base64_encode($this->smtpOptions['username']), '334')) { return false; } if (!$this->__smtpSend(base64_encode($this->smtpOptions['password']), '235')) { return false; } } if (!$this->__smtpSend('MAIL FROM: ' . $this->__formatAddress($this->from, true))) { return false; } if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($this->to, true))) { return false; } foreach ($this->cc as $cc) { if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($cc, true))) { return false; } } foreach ($this->bcc as $bcc) { if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($bcc, true))) { return false; } } if (!$this->__smtpSend('DATA', '354')) { return false; } if (!$this->__smtpSend($this->__header . "\r\n\r\n" . $this->__message . "\r\n\r\n\r\n.")) { return false; } $this->__smtpSend('QUIT', false); $this->__smtpConnection->disconnect(); return true; }/** * Private method for sending data to SMTP connection * * @param string $data data to be sent to SMTP server * @param mixed $checkCode code to check for in server response, false to skip * @return bool Success * @access private */ function __smtpSend($data, $checkCode = '250') { if (!is_null($data)) { $this->__smtpConnection->write($data . "\r\n"); } if ($checkCode !== false) { $response = ''; while ($str = $this->__smtpConnection->read()) { $response .= $str; if ($str[3] == ' ') { break; } } if (stristr($response, $checkCode) === false) { $this->smtpError = $response; return false; } } return true; }/** * Set as controller flash message a debug message showing current settings in component * * @return boolean Success * @access private */ function __debug() { $nl = $this->_newLine; $fm = '<pre>'; if ($this->delivery == 'smtp') { $fm .= sprintf('%s %s%s', 'Host:', $this->smtpOptions['host'], $nl); $fm .= sprintf('%s %s%s', 'Port:', $this->smtpOptions['port'], $nl); $fm .= sprintf('%s %s%s', 'Timeout:', $this->smtpOptions['timeout'], $nl); } $fm .= sprintf('%s %s%s', 'To:', $this->to, $nl); $fm .= sprintf('%s %s%s', 'From:', $this->from, $nl); $fm .= sprintf('%s %s%s', 'Subject:', $this->subject, $nl); $fm .= sprintf('%s%3$s%3$s%s', 'Header:', $this->__header, $nl); $fm .= sprintf('%s%3$s%3$s%s', 'Parameters:', $this->additionalParams, $nl); $fm .= sprintf('%s%3$s%3$s%s', 'Message:', $this->__message, $nl); $fm .= '</pre>'; $this->Controller->Session->setFlash($fm, 'default', null, 'email'); return true; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -