/** * Authenticates the user to the IMAP server with $user and $password. * * This method should be called directly after the construction of this * object. * * If the server is waiting for the authentication process to respond, the * connection with the IMAP server will be closed, and false is returned, * and it is the application's task to reconnect and reauthenticate. * * Example of creating an IMAP transport and authenticating: * <code> * // replace with your IMAP server address * $imap = new ezcMailImapTransport( 'imap.example.com' ); * * // replace the values with your username and password for the IMAP server * $imap->authenticate( 'username', 'password' ); * </code> * * @throws ezcMailTransportException * if already authenticated * or if the provided username/password combination did not work * @param string $user * @param string $password * @return bool */ public function authenticate($user, $password) { if ($this->state != self::STATE_NOT_AUTHENTICATED) { throw new ezcMailTransportException("Tried to authenticate when there was no connection or when already authenticated."); } $tag = $this->getNextTag(); $this->connection->sendData("{$tag} LOGIN {$user} {$password}"); $response = trim($this->connection->getLine()); // hack for gmail, to fix issue #15837: imap.google.com (google gmail) changed IMAP response if ($this->serverType === self::SERVER_GIMAP && strpos($response, "* CAPABILITY") === 0) { $response = trim($this->connection->getLine()); } if (strpos($response, '* OK') !== false) { // the server is busy waiting for authentication process to // respond, so it is a good idea to just close the connection, // otherwise the application will be halted until the server // recovers $this->connection->close(); $this->connection = null; $this->state = self::STATE_NOT_CONNECTED; return false; } if ($this->responseType($response) != self::RESPONSE_OK) { throw new ezcMailTransportException("The IMAP server did not accept the username and/or password: {$response}."); } else { $this->state = self::STATE_AUTHENTICATED; $this->selectedMailbox = null; } return true; }
/** * Disconnects the transport from the POP3 server. */ public function disconnect() { if ($this->state != self::STATE_NOT_CONNECTED) { $this->connection->sendData('QUIT'); $this->connection->getLine(); // discard $this->state = self::STATE_UPDATE; $this->connection->close(); $this->connection = null; $this->state = self::STATE_NOT_CONNECTED; } }