/** * Write data to the cache * @param string The cache key * @param string The data to write */ function write($key, $data) { $handle = @fopen($GLOBALS["_SWIFT_FILECACHE_SAVE_PATH_"] . "/" . $this->prefix . $key, "ab"); if (false === $handle || false === fwrite($handle, $data)) { Swift_ClassLoader::load("Swift_FileException"); Swift_ClassLoader::load("Swift_Errors"); Swift_Errors::trigger(new Swift_FileException("Disk Caching failed. Tried to write to file at [" . $GLOBALS["_SWIFT_FILECACHE_SAVE_PATH_"] . "/" . $this->prefix . $key . "] but failed. Check the permissions, or don't use disk caching.")); return; } fclose($handle); }
/** * Try to authenticate using the username and password * Returns false on failure * @param string The username * @param string The password * @param Swift The instance of Swift this authenticator is used in * @return boolean */ function isAuthenticated($user, $pass, &$swift) { //The authorization string uses ascii null as a separator (See RFC 2554) $credentials = base64_encode($user . chr(0) . $user . chr(0) . $pass); Swift_ClassLoader::load("Swift_Errors"); Swift_Errors::expect($e, "Swift_ConnectionException"); $swift->command("AUTH PLAIN " . $credentials, 235); if ($e) { $swift->reset(); return false; } Swift_Errors::clear("Swift_ConnectionException"); return true; }
/** * Try to authenticate using the username and password * Returns false on failure * @param string The username * @param string The password * @param Swift The instance of Swift this authenticator is used in * @return boolean */ function isAuthenticated($user, $pass, &$swift) { $log =& Swift_LogContainer::getLog(); if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) { $log->add("Trying POP3 Before SMTP authentication. Disconnecting from SMTP first."); } $swift->disconnect(); $this->connection->start(); Swift_Errors::expect($e, "Swift_ConnectionException"); if (!$e) { $this->connection->assertOk($this->connection->read()); } if (!$e) { $this->connection->write("USER " . $user); } if (!$e) { $this->connection->assertOk($this->connection->read()); } if (!$e) { $this->connection->write("PASS " . $pass); } if (!$e) { $this->connection->assertOk($this->connection->read()); } if (!$e) { $this->connection->write("QUIT"); } if (!$e) { $this->connection->assertOk($this->connection->read()); } if (!$e) { $this->connection->stop(); } if ($e) { if ($log->hasLevel(SWIFT_LOG_ERRORS)) { $log->add("POP3 authentication failed."); } return false; } Swift_Errors::clear("Swift_ConnectionException"); $options = $swift->getOptions(); $swift->setOptions($options | SWIFT_NO_POST_CONNECT); $swift->connect(); $swift->setOptions($options); return true; }
/** * Try to authenticate using the username and password * Returns false on failure * @param string The username * @param string The password * @param Swift The instance of Swift this authenticator is used in * @return boolean */ function isAuthenticated($user, $pass, &$swift) { Swift_ClassLoader::load("Swift_Errors"); Swift_Errors::expect($e, "Swift_ConnectionException"); $res =& $swift->command("AUTH CRAM-MD5", 334); if (!$e && instance_of($res, 'Swift_Events_ResponseEvent')) { $encoded_challenge = substr($res->getString(), 4); $challenge = base64_decode($encoded_challenge); $response = base64_encode($user . " " . $this->generateCRAMMD5Hash($pass, $challenge)); $swift->command($response, 235); } if ($e !== null) { $swift->reset(); return false; } Swift_Errors::clear("Swift_ConnectionException"); return true; }
/** * Set data for the image * This overrides setData() in Swift_Message_Attachment * @param Swift_File The data to set, as a file * @throws Swift_Message_MimeException If the image cannot be used, or the file is not */ function setData(&$data, $read_filename = true) { if (!is_a($data, "Swift_File")) { trigger_error("Parameter 1 of " . __CLASS__ . "::" . __FUNCTION__ . " must be instance of Swift_File"); return; } parent::setData($data, $read_filename); $img_data = @getimagesize($data->getPath()); if (!$img_data) { Swift_Errors::trigger(new Swift_Message_MimeException("Cannot use file '" . $data->getPath() . "' as image since getimagesize() was unable to detect a file format. " . "Try using Swift_Message_EmbeddedFile instead")); return; } $type = image_type_to_mime_type($img_data[2]); $this->setContentType($type); if (!$this->getFileName()) { $this->setFileName($data->getFileName()); } }
/** * Try to authenticate using the username and password * Returns false on failure * @param string The username * @param string The password * @param Swift The instance of Swift this authenticator is used in * @return boolean */ function isAuthenticated($user, $pass, &$swift) { Swift_ClassLoader::load("Swift_Errors"); Swift_Errors::expect($e, "Swift_ConnectionException"); if (!$e) { $swift->command("AUTH LOGIN", 334); } if (!$e) { $swift->command(base64_encode($user), 334); } if (!$e) { $swift->command(base64_encode($pass), 235); } if ($e) { $swift->reset(); return false; } Swift_Errors::clear("Swift_ConnectionException"); return true; }
/** * Try to start the connection * @throws Swift_ConnectionException Upon failure to start */ function start() { $log =& Swift_LogContainer::getLog(); if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) { $log->add("Trying to start a sendmail process."); } if (!$this->getPath() || !$this->getFlags()) { Swift_Errors::trigger(new Swift_ConnectionException("Sendmail cannot be started without a path to the binary including flags.")); return; } if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) { $log->add("Trying to stat the executable '" . $this->getPath() . "'."); } if (!@lstat($this->getPath())) { Swift_Errors::trigger(new Swift_ConnectionException("Sendmail cannot be seen with lstat(). The command given [" . $this->getCommand() . "] does not appear to be valid.")); return; } $pipes_spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")); $i = count($GLOBALS["_SWIFT_PROC"]); $GLOBALS["_SWIFT_PROC"][$i] = proc_open($this->getCommand(), $pipes_spec, $this->pipes); $this->proc =& $GLOBALS["_SWIFT_PROC"][$i]; if (!$this->isAlive()) { Swift_Errors::trigger(new Swift_ConnectionException("The sendmail process failed to start. Please verify that the path exists and PHP has permission to execute it.")); return; } }
/** * Write a command to the server (leave off trailing CRLF) * @param string The command to send * @throws Swift_ConnectionException Upon failure to write */ function write($command, $end = "\r\n") { if ($this->active === null) { Swift_Errors::trigger(new Swift_ConnectionException("None of the connections set have been started")); return; } return $this->connections[$this->active]->write($command, $end); }
/** * Send a message to any number of recipients * @param Swift_Message The message to send. This does not need to (and shouldn't really) have any of the recipient headers set. * @param mixed The recipients to send to. Can be a string, Swift_Address or Swift_RecipientList. Note that all addresses apart from Bcc recipients will appear in the message headers * @param mixed The address to send the message from. Can either be a string or an instance of Swift_Address. * @return int The number of successful recipients * @throws Swift_ConnectionException If sending fails for any reason. */ function send(&$message, $recipients, $from) { Swift_ClassLoader::load("Swift_Message_Encoder"); $encoder =& Swift_Message_Encoder::instance(); if (!is_a($message, "Swift_Message")) { trigger_error("Swift::send expects parameter 1 to be instance of Swift_Message."); return; } if (is_string($recipients) && preg_match("/^" . $encoder->CHEAP_ADDRESS_RE . "\$/", $recipients)) { $recipients =& new Swift_Address($recipients); } elseif (!is_a($recipients, "Swift_AddressContainer")) { trigger_error("The recipients parameter must either be a valid string email address, " . "an instance of Swift_RecipientList or an instance of Swift_Address."); return; } if (is_string($from) && preg_match("/^" . $encoder->CHEAP_ADDRESS_RE . "\$/", $from)) { $from =& new Swift_Address($from); } elseif (!is_a($from, "Swift_Address")) { trigger_error("The sender parameter must either be a valid string email address or " . "an instance of Swift_Address."); return; } $log =& Swift_LogContainer::getLog(); if (!$message->getEncoding() && !$this->connection->hasExtension("8BITMIME")) { $message->setEncoding("QP", true, true); } $list =& $recipients; if (is_a($recipients, "Swift_Address")) { $list =& new Swift_RecipientList(); $list->addTo($recipients); } Swift_ClassLoader::load("Swift_Events_SendEvent"); $send_event =& new Swift_Events_SendEvent($message, $list, $from, 0); $this->notifyListeners($send_event, "BeforeSendListener"); $to = $cc = array(); if (!($has_from = $message->getFrom())) { $message->setFrom($from); } if (!($has_return_path = $message->getReturnPath())) { $message->setReturnPath($from->build(true)); } if (!($has_reply_to = $message->getReplyTo())) { $message->setReplyTo($from); } if (!($has_message_id = $message->getId())) { $message->generateId(); } $this->command("MAIL FROM: " . $message->getReturnPath(true), 250); $failed = 0; $sent = 0; $tmp_sent = 0; $it =& $list->getIterator("to"); while ($it->hasNext()) { $it->next(); $address = $it->getValue(); $to[] = $address->build(); $e = null; Swift_Errors::expect($e, "Swift_BadResponseException"); $this->command("RCPT TO: " . $address->build(true), 250); if (!$e) { $tmp_sent++; Swift_Errors::clear("Swift_BadResponseException"); } else { $failed++; $send_event->addFailedRecipient($address->getAddress()); if ($log->hasLevel(SWIFT_LOG_FAILURES)) { $log->addfailedRecipient($address->getAddress()); } } } $it =& $list->getIterator("cc"); while ($it->hasNext()) { $it->next(); $address = $it->getValue(); $cc[] = $address->build(); $e = null; Swift_Errors::expect($e, "Swift_BadResponseException"); $this->command("RCPT TO: " . $address->build(true), 250); if (!$e) { $tmp_sent++; Swift_Errors::clear("Swift_BadResponseException"); } else { $failed++; $send_event->addFailedRecipient($address->getAddress()); if ($log->hasLevel(SWIFT_LOG_FAILURES)) { $log->addfailedRecipient($address->getAddress()); } } } if ($failed == count($to) + count($cc)) { $this->reset(); $this->notifyListeners($send_event, "SendListener"); return 0; } if (!($has_to = $message->getTo()) && !empty($to)) { $message->setTo($to); } if (!($has_cc = $message->getCc()) && !empty($cc)) { $message->setCc($cc); } $this->command("DATA", 354); $data =& $message->build(); while (false !== ($bytes = $data->read())) { $this->command($bytes, -1); } if ($log->hasLevel(SWIFT_LOG_NETWORK)) { $log->add("<MESSAGE DATA>", SWIFT_LOG_COMMAND); } Swift_Errors::expect($e, "Swift_BadResponseException"); $this->command("\r\n.", 250); if (!$e) { $sent += $tmp_sent; Swift_Errors::clear("Swift_BadResponseException"); } else { $failed += $tmp_sent; } $tmp_sent = 0; $has_bcc = $message->getBcc(); $it =& $list->getIterator("bcc"); while ($it->hasNext()) { $it->next(); $address = $it->getValue(); if (!$has_bcc) { $message->setBcc($address->build()); } $e = null; Swift_Errors::expect($e, "Swift_BadResponseException"); if (!$e) { $this->command("MAIL FROM: " . $message->getReturnPath(true), 250); } if (!$e) { $this->command("RCPT TO: " . $address->build(true), 250); } if (!$e) { $this->command("DATA", 354); } if (!$e) { $data =& $message->build(); while (false !== ($bytes = $data->read())) { $this->command($bytes, -1); } if ($log->hasLevel(SWIFT_LOG_NETWORK)) { $log->add("<MESSAGE DATA>", SWIFT_LOG_COMMAND); } $this->command("\r\n.", 250); } if (!$e) { $sent++; Swift_Errors::clear("Swift_BadResponseException"); } else { $failed++; $send_event->addFailedRecipient($address->getAddress()); if ($log->hasLevel(SWIFT_LOG_FAILURES)) { $log->addfailedRecipient($address->getAddress()); } $this->reset(); } } $total = count($to) + count($cc) + count($list->getBcc()); $send_event->setNumSent($sent); $this->notifyListeners($send_event, "SendListener"); if (!$has_return_path) { $message->setReturnPath(""); } if (!$has_from) { $message->setFrom(""); } if (!$has_to) { $message->setTo(""); } if (!$has_reply_to) { $message->setReplyTo(null); } if (!$has_cc) { $message->setCc(null); } if (!$has_bcc) { $message->setBcc(null); } if (!$has_message_id) { $message->setId(null); } if ($log->hasLevel(SWIFT_LOG_NETWORK)) { $log->add("Message sent to " . $sent . "/" . $total . " recipients", SWIFT_LOG_NORMAL); } return $sent; }
/** * Send an email to a number of recipients * Returns the number of successful recipients, or FALSE on failure * @param mixed The recipients to send to. One of string, array, 2-dimensional array or Swift_Address * @param mixed The address to send from. string or Swift_Address * @param string The message subject * @param string The message body, optional * @return int */ function send($recipients, $from, $subject, $body = null) { $this->addTo($recipients); $sender = false; if (is_string($from)) { $sender = $this->stringToAddress($from); } elseif (is_a($from, "Swift_Address")) { $sender =& $from; } if (!$sender) { return false; } $this->message->setSubject($subject); if ($body) { $this->message->setBody($body); } $sent = 0; Swift_Errors::expect($e, "Swift_ConnectionException"); if (!$this->exactCopy && !$this->recipients->getCc() && !$this->recipients->getBcc()) { $sent = $this->swift->batchSend($this->message, $this->recipients, $sender); } else { $sent = $this->swift->send($this->message, $this->recipients, $sender); } if (!$e) { Swift_Errors::clear("Swift_ConnectionException"); if ($this->autoFlush) { $this->flush(); } return $sent; } $this->setError("Sending failed:<br />" . $e->getMessage()); return false; }
/** * Try to start the connection * @throws Swift_ConnectionException Upon failure to start */ function start() { $log =& Swift_LogContainer::getLog(); $fail_messages = array(); foreach ($this->connections as $id => $conn) { Swift_Errors::expect($e, "Swift_ConnectionException"); // $this->connections[$id]->start(); if (!$e) { if ($this->connections[$id]->isAlive()) { Swift_Errors::clear("Swift_ConnectionException"); $this->active = $id; return true; } if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) { $log->add("Connection (" . $id . ") failed. Will try next connection if available."); } Swift_Errors::trigger(new Swift_ConnectionException("The connection started but reported that it was not active")); } $fail_messages[] = $id . ": " . $e->getMessage(); $e = null; } $failure = implode("<br />", $fail_messages); Swift_Errors::trigger(new Swift_ConnectionException($failure)); }
function gmail(&$error, $to, $subject, $body, $fromName = "", $html = 0, $repto = "", $from = "", $cc = "", $reptoName = "") { global $errorReportingLevel; require_once GORUM_DIR . "/SwiftMailer/Swift.php"; $_S =& new AppSettings(); error_reporting(0); $log =& Swift_LogContainer::getLog(); $log->setLogLevel($_S->swiftLog); Swift_Errors::expect($e, "Swift_Exception"); if ($_S->smtpServer) { require_once GORUM_DIR . "/SwiftMailer/Swift/Connection/SMTP.php"; if ($_S->fallBackNative) { require_once GORUM_DIR . "/SwiftMailer/Swift/Connection/NativeMail.php"; require_once GORUM_DIR . "/SwiftMailer/Swift/Connection/Multi.php"; $connections = array(); $conn =& new Swift_Connection_SMTP($_S->smtpServer); $conn->setUsername($_S->smtpUser); $conn->setPassword($_S->smtpPass); $conn->setPort($_S->smtpPort); $conn->setPort($_S->smtpPort); $conn->setEncryption($_S->smtpSecure); if ($e !== null) { $error = $e->getMessage(); return; } $connections[] =& $conn; // Fall back to native mail: $connections[] =& new Swift_Connection_NativeMail(); if ($e !== null) { $error = $e->getMessage(); return; } $swift =& new Swift(new Swift_Connection_Multi($connections)); } else { $connection =& new Swift_Connection_SMTP($_S->smtpServer); $connection->setUsername($_S->smtpUser); $connection->setPassword($_S->smtpPass); $connection->setPort($_S->smtpPort); $connection->setEncryption($_S->smtpSecure); if ($e !== null) { $error = $e->getMessage(); return; } $swift =& new Swift($connection); } } else { require_once GORUM_DIR . "/SwiftMailer/Swift/Connection/NativeMail.php"; $swift =& new Swift(new Swift_Connection_NativeMail()); } error_reporting($errorReportingLevel); if ($e !== null) { $error = $e->getMessage(); return; } else { $error = ""; Swift_Errors::clear("Swift_Exception"); } $subject = str_replace(array("<", ">"), array("<", ">"), $subject); $charset = "utf-8"; $message =& new Swift_Message($subject); $message->setCharset($charset); $part1 =& new Swift_Message_Part($body, "text/html"); $part1->setCharset($charset); $message->attach($part1); $part2 =& new Swift_Message_Part(strip_tags($body)); $part2->setCharset($charset); $message->attach($part2); if ($repto) { $message->setReplyTo(new Swift_Address($repto, $reptoName)); } error_reporting(0); Swift_Errors::expect($e, "Swift_Exception"); $recipients = new Swift_RecipientList(); $recipients->addTo($to); if ($this->cc) { $recipients->addCc($this->cc); } $swift->send($message, $recipients, new Swift_Address($from, $fromName)); if ($e !== null) { $error = $e->getMessage(); } else { $error = ""; Swift_Errors::clear("Swift_Exception"); } $swift->disconnect(); error_reporting($errorReportingLevel); }
/** * Get a child document, identified by $id * @param string The identifier for this child * @return Swift_Message_Mime The child document * @throws Swift_Message_MimeException If no such child exists */ function &getChild($id) { if ($this->hasChild($id)) { return $this->children[$id]; } else { Swift_Errors::trigger(new Swift_Message_MimeException("Cannot retrieve child part identified by '" . $id . "' as it does not exist. Consider using hasChild() to check.")); } }
/** * Write a command to the remote socket * @param string the command to send (without CRLF) * @throws Swift_ConnectionException If the command cannot be written */ function write($command) { if (false !== fwrite($this->handle, $command . "\r\n")) { Swift_Errors::trigger(new Swift_ConnectionException("Data could not be written to the POP3 connection.")); return; } }
/** * Establish an open file handle on the file if the file is not yet opened * @throws Swift_FileException If the file cannot be opened for reading */ function createHandle() { if ($this->handle === null) { if (!($this->handle = fopen($this->path, "rb"))) { Swift_Errors::trigger(new Swift_FileException("Unable to open file '" . $this->path . " for reading. Check the file permissions.")); return; } } }
/** * Run a batch send in a fail-safe manner. * This operates as Swift::batchSend() except it deals with errors itself. * @param Swift_Message To send * @param Swift_RecipientList Recipients (To: only) * @param Swift_Address The sender's address * @return int The number sent to */ function send(&$message, &$recipients, $sender, $modified_subject = array()) { $sent = 0; $successive_fails = 0; set_error_handler(array(&$this, "handleError")); foreach ($recipients->getTo() as $recipient) { $loop = true; $tries = 0; while ($loop && $tries < $this->getMaxTries()) { $tries++; $loop = false; $this->copyMessageHeaders($message); //BOF:mod if (!empty($modified_subject)) { $temp = $modified_subject[$recipient->getAddress()]; if (!empty($temp)) { $message->setSubject($temp); } } //EOF:mod $sent += $n = $this->swift->send($message, $recipient, $sender, $modified_subject); if (!$n) { $this->addFailedRecipient($recipient->getAddress()); } if ($this->doRestart) { $successive_fails++; $this->restoreMessageHeaders($message); if (($max = $this->getMaxSuccessiveFailures()) && $successive_fails > $max) { restore_error_handler(); Swift_Errors::trigger(new Swift_Exception("Too many successive failures. BatchMailer is configured to allow no more than " . $max . " successive failures.")); return; } $loop = true; //Give it one more shot if ($t = $this->getSleepTime()) { sleep($t); } $this->forceRestartSwift(); } else { $successive_fails = 0; } } } restore_error_handler(); return $sent; }
/** * The last error message as a string * @return string */ function getLast() { $me =& Swift_Errors::getInstance(); if (count($me->errors)) { $last =& $me->errors[count($me->errors) - 1]; return $last->getMessage(); } }
/** * Try to close the connection * @throws Swift_ConnectionException Upon failure to close */ function stop() { $log =& Swift_LogContainer::getLog(); if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) { $log->add("Closing down SMTP connection."); } if ($this->handle) { if (!fclose($this->handle)) { Swift_Errors::trigger(new Swift_ConnectionException("The SMTP connection could not be closed for an unknown reason." . $this->smtpErrors())); return; } $this->handle = null; } }
function doMail($to, $subject, $message, $headers, $params) { $original_from = @ini_get("sendmail_from"); @ini_set("sendmail_from", $this->returnPath); $headers = $headers->build(); if (!ini_get("safe_mode")) { $success = mail($to, $subject, $message, $headers, $params); } else { $success = mail($to, $subject, $message, $headers); } if (!$success) { @ini_set("sendmail_from", $original_from); Swift_Errors::trigger(new Swift_ConnectionException("Sending failed using mail() as PHP's default mail() function returned boolean FALSE.")); return; } @ini_set("sendmail_from", $original_from); }
/** * Get the value for a given attribute on a given header * @param string The name of the main header * @param string The name of the attribute * @return string * @throws Swift_Message_MimeException If no header is set */ function getAttribute($header, $name) { if (!$this->has($header)) { Swift_Errors::trigger(new Swift_Message_MimeException("Cannot locate attribute '" . $name . "' for header '" . $header . "' as the header does not exist. " . "Consider using Swift_Message_Headers->has() to check.")); return; } $name = strtolower($name); $lheader = strtolower($header); if ($this->hasAttribute($header, $name)) { return $this->attributes[$lheader][$name]; } }
/** * Execute needed logic prior to compilation */ function preBuild() { if (Swift_Errors::halted()) { return; } $data =& $this->getData(); if (!($enc = $this->getEncoding())) { $this->setEncoding("8bit"); } if ($this->getCharset() === null && !$this->numChildren()) { if (is_string($data) && $this->_encoder->isUTF8($data)) { $this->setCharset("utf-8"); } elseif (is_string($data) && $this->_encoder->is7BitAscii($data)) { $this->setCharset("us-ascii"); if (!$enc) { $this->setEncoding("7bit"); } } else { $this->setCharset("iso-8859-1"); } } elseif ($this->numChildren()) { if (!$data) { $this->setData($this->getMimeWarning()); $this->setLineWrap(76); } if ($this->getCharset() !== null) { $this->setCharset(null); } if ($this->isFlowed()) { $this->setFlowed(false); } $this->setEncoding("7bit"); } }