Beispiel #1
0
 /**
  * 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;
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
0
 /**
  * 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;
 }
Beispiel #5
0
 /**
  * Rotate to the next working connection
  * @throws Swift_ConnectionException If no connections are available
  */
 function nextConnection()
 {
     $log =& Swift_LogContainer::getLog();
     if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) {
         $log->add(" <==> Rotating connection.");
     }
     $total = count($this->connections);
     $start = $this->active === null ? 0 : $this->active + 1;
     if ($start >= $total) {
         $start = 0;
     }
     $fail_messages = array();
     for ($id = $start; $id < $total; $id++) {
         if (in_array($id, $this->dead)) {
             continue;
         }
         //The connection was previously found to be useless
         Swift_Errors::expect($e, "Swift_ConnectionException");
         if (!$this->connections[$id]->isAlive()) {
             $this->connections[$id]->start();
         }
         if (!$e) {
             if ($this->connections[$id]->isAlive()) {
                 Swift_Errors::clear("Swift_ConnectionException");
                 $this->active = $id;
                 return true;
             }
             $this->dead[] = $id;
             $this->connections[$id]->stop();
             Swift_Errors::trigger(new Swift_ConnectionException("The connection started but reported that it was not active"));
         }
         $this->dead[] = $id;
         $this->connections[$id]->stop();
         $fail_messages[] = $id . ": " . $e->getMessage();
         $e = null;
     }
     $failure = implode("<br />", $fail_messages);
     Swift_Errors::trigger(new Swift_ConnectionException("No connections were started.<br />" . $failure));
 }
Beispiel #6
0
 /**
  * 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;
 }
Beispiel #8
0
 /**
  * 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));
 }
Beispiel #9
0
 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("&lt;", "&gt;"), 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);
 }
Beispiel #10
0
 /**
  * Remove a nested MIME part
  * @param string The ID of the attached part
  * @throws Swift_Message_MimeException If no such part exists
  */
 function detach($id)
 {
     if (Swift_Errors::halted()) {
         return;
     }
     Swift_Errors::expect($e, "Swift_Message_MimeException");
     //try
     switch (true) {
         case array_key_exists($id, $this->references["alternative"]):
             $parentRef =& $this->getReference("parent", "alternative");
             $parentRef->removeChild($id);
             unset($this->references["alternative"][$id]);
             break;
         case array_key_exists($id, $this->references["related"]):
             $parentRef =& $this->getReference("parent", "related");
             $parentRef->removeChild($id);
             unset($this->references["related"][$id]);
             break;
         case array_key_exists($id, $this->references["mixed"]):
             $parentRef =& $this->getReference("parent", "mixed");
             $parentRef->removeChild($id);
             unset($this->references["mixed"][$id]);
             break;
         default:
             trigger_error("Unable to detach part identified by ID '" . $id . "' since it's not registered.");
             break;
     }
     $this->postDetachFixStructure();
     $this->fixContentType();
     //catch
     if ($e) {
         Swift_Errors::trigger(new Swift_Message_MimeException("Something went wrong whilst trying to move some MIME parts during a detach(). " . "The MIME component threw an exception:<br />" . $e->getMessage()));
     } else {
         Swift_Errors::clear("Swift_Message_MimeException");
     }
 }