📄 swift.php
字号:
return $this->Bcc; } /** * Get To addresses */ public function getToAddresses() { return $this->to; } /** * Get the list of failed recipients * @return array recipients */ public function getFailedRecipients() { return $this->failedAddresses; } /** * Return the array of errors (if any) * @return array errors */ public function getErrors() { return $this->errors; } /** * Return the conversation up to maxLogSize between the SMTP server and swift * @return array transactions */ public function getTransactions() { return $this->transactions; } /** * Sets the Reply-To address used for sending mail * @param string address */ public function setReplyTo($string) { $this->replyTo = $this->getAddress($string); } /** * Add one or more Blind-carbon-copy recipients to the mail * @param mixed addresses */ public function addBcc($addresses) { $this->Bcc = array_merge($this->Bcc, $this->parseAddressList((array) $addresses)); } /** * Add one or more Carbon-copy recipients to the mail * @param mixed addresses */ public function addCc($addresses) { $this->Cc = array_merge($this->Cc, $this->parseAddressList((array) $addresses)); } /** * Force swift to break lines longer than 76 characters long * @param bool resize */ public function useAutoLineResizing($use=true) { $this->autoCompliance = (bool) $use; } /** * Associate a code with a command. Swift will fail quietly if the code * returned does not match. * @param string command * @param int code */ public function addExpectedCode($command, $code) { $this->expectedCodes[$command] = (int) $code; } /** * Reads the EHLO return string to see what AUTH methods are supported * @param string EHLO response * @return void * @private */ private function getAuthenticationMethods($list) { preg_match("/^250[\-\ ]AUTH\ (.*)\r\n/m", $list, $matches); if (!empty($matches[1])) { $types = explode(' ', $matches[1]); $this->authTypes = $types; } } /** * Load a plugin object into Swift * @param object Swift_IPlugin * @param string plugin name * @return void */ public function loadPlugin(Swift_IPlugin &$object, $id=false) { if ($id) $object->pluginName = $id; $this->plugins[$object->pluginName] =& $object; $this->plugins[$object->pluginName]->loadBaseObject($this); if (method_exists($this->plugins[$object->pluginName], 'onLoad')) { $this->plugins[$object->pluginName]->onLoad(); } } /** * Fetch a reference to a plugin in Swift * @param string plugin name * @return object Swift_IPlugin */ public function &getPlugin($name) { if (isset($this->plugins[$name])) { return $this->plugins[$name]; } } /** * Un-plug a loaded plugin. Returns false on failure. * @param string plugin_name * @return bool success */ public function removePlugin($name) { if (!isset($this->plugins[$name])) return false; if (method_exists($this->plugins[$name], 'onUnload')) { $this->plugins[$name]->onUnload(); } unset($this->plugins[$name]); return true; } /** * Return the number of plugins loaded * @return int plugins */ public function numPlugins() { return count($this->plugins); } /** * Trigger event handlers * @param string event handler * @return void * @private */ private function triggerEventHandler($func) { foreach ($this->plugins as $name => $object) { if (method_exists($this->plugins[$name], $func)) { $this->plugins[$name]->$func(); } } } /** * Attempt to load any authenticators from the Swift/ directory * @see RFC 2554 * @return void * @private */ private function loadDefaultAuthenticators() { $dir = dirname(__FILE__).'/Swift/Authenticator'; if (file_exists($dir) && is_dir($dir)) { $handle = opendir($dir); while ($file = readdir($handle)) { if (preg_match('@^([a-zA-Z\d]*)\.php$@', $file, $matches)) { require_once($dir.'/'.$file); $class = 'Swift_Authenticator_'.$matches[1]; $this->loadAuthenticator(new $class); } } closedir($handle); } } /** * Use SMTP authentication * @param string username * @param string password * @return bool successful */ public function authenticate($username, $password) { $this->username = $username; $this->password = $password; if (empty($this->authenticators)) $this->loadDefaultAuthenticators(); if (!$this->esmtp || empty($this->authTypes)) { $this->logError('The MTA doesn\'t support any of Swift\'s loaded authentication mechanisms', 0); return false; } foreach ($this->authenticators as $name => $object) { //An asterisk means that the auth type is not advertised by ESMTP if (in_array($name, $this->authTypes) || substr($name, 0, 1) == '*') { if ($this->authenticators[$name]->run($username, $password)) { $this->triggerEventHandler('onAuthenticate'); return true; } else return false; } } //If we get this far, no authenticators were used $this->logError('The MTA doesn\'t support any of Swift\'s loaded authentication mechanisms', 0); $this->fail(); return false; } /** * Load an authentication mechanism object into Swift * @param object Swift_IAuthenticator * @return void */ public function loadAuthenticator(Swift_IAuthenticator &$object) { $this->authenticators[$object->serverString] =& $object; $this->authenticators[$object->serverString]->loadBaseObject($this); } /** * Get a unique multipart MIME boundary * @param string mail data, optional * @return string boundary * @private */ private function getMimeBoundary($string=false) { $force = true; if (!$string) { $force = false; $string = implode('', $this->parts); $string .= implode('', $this->attachments); } if ($this->mimeBoundary && !$force) return $this->mimeBoundary; else { //Make sure we don't (as if it would ever happen!) - // produce a boundary that's actually in the email already do { $this->mimeBoundary = '_=_swift-'.uniqid(rand(), true); } while(strpos($string, $this->mimeBoundary)); } return $this->mimeBoundary; } /** * Append a string to the message header * @param string headers * @return void */ public function addHeaders($string) { $this->headers .= preg_replace("/(?:\r|\n|^)[^:]*?:\ *(.*?)(?:\r|\n|$)/me", 'str_replace("$1", $this->safeEncodeHeader("$1"), "$0")', $string); if (substr($this->headers, -2) != "\r\n") $this->headers .= "\r\n"; } /** * Set the multipart MIME boundary (only works for first part) * @param string boundary * @return void */ public function setMimeBoundary($string) { $this->mimeBoundary = $string; } /** * Set the text that displays in non-MIME clients * @param string warning * @return void */ public function setMimeWarning($warning) { $this->mimeWarning = $warning; } /** * Tells Swift to clear out attachment, parts, headers etc * automatically upon sending - this is the default. * @param bool flush */ public function autoFlush($flush=true) { $this->autoFlush = (bool) $flush; } /** * Empty out the MIME parts and attachments * @param bool reset headers * @return void */ public function flush($clear_headers=false) { $this->parts = array(); $this->attachments = array(); $this->images = array(); $this->mimeBoundary = null; $this->Bcc = array(); $this->to = array(); $this->Cc = array(); $this->replyTo = null; //See comment above the headers property above the constructor before editing this line! * if ($clear_headers) $this->headers = "X-Mailer: Swift ".SWIFT_VERSION." by Chris Corbyn\r\n"; $this->triggerEventHandler('onFlush'); } /** * Reset to */ public function flushTo() { $this->to = array(); } /** * Reset Cc */ public function flushCc() { $this->Cc = array(); } /** * Reset Bcc */ public function flushBcc() { $this->Bcc = array(); } /** * Reset parts */ public function flushParts() { $this->parts = array(); $this->images = array(); } /** * Reset attachments */ public function flushAttachments() { $this->attachments = array(); } /** * Reset headers */ public function flushHeaders() { $this->headers = "X-Mailer: Swift ".SWIFT_VERSION." by Chris Corbyn\r\n"; } /** * Log an error in Swift::errors * @param string error string * @param int error number * @return void */ public function logError($errstr, $errno=0) { $this->errors[] = array( 'num' => $errno, 'time' => microtime(), 'message' => $errstr ); $this->lastError = $errstr; $this->triggerEventHandler('onError'); } /** * Log a transaction in Swift::transactions * @param string command * @return void */ public function logTransaction($command='') { $this->lastTransaction = array( 'command' => $command, 'time' => microtime(), 'response' => $this->getResponse() ); $this->triggerEventHandler('onLog'); if ($this->maxLogSize) { $this->transactions = array_slice(array_merge($this->transactions, array($this->lastTransaction)), -$this->maxLogSize); } else $this->transactions[] = $this->lastTransaction; } /** * Read the data from the socket * @return string response * @private */ private function getResponse() { if (!$this->connection->readHook || !$this->isConnected()) return false; $ret = ""; while (true) { $tmp = @fgets($this->connection->readHook); $ret .= $tmp; //The last line of SMTP replies have a space after the status number // They do NOT have an EOF so while(!feof($socket)) will hang! if (substr($tmp, 3, 1) == ' ' || $tmp == false) break; } $this->responseCode = $this->getResponseCode($ret); $this->lastResponse = $ret; $this->triggerEventHandler('onResponse'); return $this->lastResponse; } /** * Get the number of the last server response * @param string response string * @return int response code * @private */ private function getResponseCode($string) { return (int) sprintf("%d", $string); } /** * Get the first word of the command * @param string command * @return string keyword * @private */ private function getCommandKeyword($comm) { if (false !== $pos = strpos($comm, ' ')) { return $this->commandKeyword = strtolower(substr($comm, 0, $pos)); } else return $this->commandKeyword = strtolower(trim($comm)); } /** * Send a reset command in the event of a problem */ public function reset() { $this->command("RSET\r\n"); } /** * Issue a command to the socket * @param string command * @return string response */ public function command($comm) { //We'll usually ignore a certain sequence of commands if something screwed up if ($this->ignoreCommands) { $this->skippedCommands++; if ($this->skippedCommands >= $this->ignoreCommands) { $this->responseCode = -2; //Done (internal to swift) $this->ignoreCommands = 0; $this->skippedCommands = 0; } return true; } $this->currentCommand = ltrim($comm); $this->triggerEventHandler('onBeforeCommand'); if (!$this->connection->writeHook || !$this->isConnected() || $this->failed) { $this->logError('Error running command: '.trim($comm).'. No connection available', 0); return false; } $command_keyword = $this->getCommandKeyword($this->currentCommand); //We successfully got as far as asking to send the email so we can forget any failed addresses for now if ($command_keyword != 'rcpt' && $command_keyword != 'rset') $this->subFailCount = 0; //SMTP commands must end with CRLF if (substr($this->currentCommand, -2) != "\r\n") $this->currentCommand .= "\r\n"; if (@fwrite($this->connection->writeHook, $this->currentCommand)) { $this->logTransaction($this->currentCommand); if (array_key_exists($command_keyword, $this->expectedCodes)) { if ($this->expectedCodes[$command_keyword] != $this->responseCode) { //If a recipient was rejected if ($command_keyword == 'rcpt') { $this->failCount++; $this->failedAddresses[] = $this->getAddress($comm); //Some addresses may still work... if (++$this->subFailCount >= $this->numAddresses) { //Sending failed, just RSET and don't send data to this recipient $this->reset(); //So we can still cache the mail body in send() $this->responseCode = -1; //Pending (internal to swift) //Skip the next two commands (DATA and <mail>) $this->ignoreCommands = 2; $this->logError('Send Error: Sending to '.$this->subFailCount.' recipients rejected (bad response code).', $this->responseCode); //But don't fail here.... these are usually not fatal } } else { $this->fail(); $this->logError('MTA Error (Swift was expecting response code '.$this->expectedCodes[$command_keyword].' but got '.$this->responseCode.'): '.$this->lastResponse, $this->responseCode); return $this->hasFailed(); } } } $this->triggerEventHandler('onCommand'); return $this->lastResponse; } else return false; } /** * Splits lines longer than 76 characters to multiple lines * @param string text * @return string chunked output */ public function chunkSplitLines($string) { return wordwrap($string, 74, "\r\n"); } /** * Add a part to a multipart message * @param string body * @param string content-type, optional * @param string content-transfer-encoding, optional * @return void */ public function addPart($string, $type='text/plain', $encoding=false) { if (!$this->userCharset && (strtoupper($this->charset) != 'UTF-8') && $this->detectUTF8($string)) $this->charset = 'UTF-8'; if (!$encoding && $this->_8bitmime) $encoding = '8bit'; elseif (!$encoding) $encoding = 'quoted-printable'; $body_string = $this->encode($string, $encoding); if ($this->autoCompliance && $encoding != 'binary') $body_string = $this->chunkSplitLines($body_string); $ret = "Content-Type: $type; charset=\"{$this->charset}\"; format=flowed\r\n". "Content-Transfer-Encoding: $encoding\r\n\r\n". $body_string; if (strtolower($type) == 'text/html') $this->parts[] = $this->makeSafe($ret); else $this->parts = array_merge((array) $this->makeSafe($ret), $this->parts); } /** * Get the current number of parts in the email * @return int num parts */ public function numParts() { return count($this->parts); } /** * Add an attachment to a multipart message. * Attachments are added as base64 encoded data. * @param string data * @param string filename * @param string content-type * @return void */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -