ezcMailImapTransport::authenticate PHP Method

authenticate() public method

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: 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' );
public authenticate ( string $user, string $password ) : boolean
$user string
$password string
return boolean
    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;
    }

Usage Example

コード例 #1
0
<?php

require_once 'tutorial_autoload.php';
// Create a new IMAP transport object by specifying the server name
$imap = new ezcMailImapTransport("imap.example.com");
// Authenticate to the IMAP server
$imap->authenticate("user", "password");
// Select the Inbox mailbox
$imap->selectMailbox('Inbox');
// List the capabilities of the IMAP server
$capabilities = $imap->capability();
// List existing mailboxes
$mailboxes = $imap->listMailboxes("", "*");
// Fetch the hierarchy delimiter character (usually "/")
$delimiter = $imap->getHierarchyDelimiter();
// Create a new mailbox
$imap->createMailbox("Reports 2006");
// Delete a mailbox
$imap->deleteMailbox("Reports 2005");
// Rename a mailbox
$imap->renameMailbox("Reports 2006", "Reports");
// Copy messages from the selected mailbox (here: Inbox) to another mailbox
$imap->copyMessages("1,2,4", "Reports");
// Set a flag to messages
// See the function description for a list of supported flags
$imap->setFlag("1,2,4", "DELETED");
// Clears a flag from messages
// See the function description for a list of supported flags
$imap->clearFlag("1,2,4", "SEEN");
// Append a message to a mailbox. $mail must contain the mail as text
// Use this with a "Sent" or "Drafts" mailbox
All Usage Examples Of ezcMailImapTransport::authenticate