create a new IMAP transport object by specifying the server name, optional port
and optional SSL mode
$options = new ezcMailImapTransportOptions();
$options->ssl = true;
$imap = new ezcMailImapTransport( 'imap.example.com', null, $options );
Authenticate to the IMAP server
$imap->authenticate( 'username', 'password' );
Select a mailbox (here 'Inbox')
$imap->selectMailbox( 'Inbox' );
issue commands to the IMAP server
for example get the number of RECENT messages
$recent = $imap->countByFlag( 'RECENT' );
see the above list of commands or consult the online documentation for
the full list of commands you can issue to an IMAP server and examples
disconnect from the IMAP server
$imap->disconnect();
See {@link ezcMailImapTransportOptions} for other options you can specify
for IMAP.
ファイルを表示
Open project: zetacomponents/mail
Class Usage Examples
Property | Type | Description | |
---|---|---|---|
$basicFlags | array(string) | Basic flags: - ANSWERED - message has been answered - DELETED - message is marked to be deleted by later EXPUNGE - DRAFT - message is marked as a draft - FLAGGED - message is "flagged" for urgent/special attention - SEEN - message has been read | |
$connection | ezcMailTransportConnection | Holds the connection to the IMAP server. | |
$currentTag | string | Used to generate a tag for sending commands to the IMAP server. | |
$extendedFlags | array(string) | Basic flags: - ANSWERED - message has been answered - DELETED - message is marked to be deleted by later EXPUNGE - DRAFT - message is marked as a draft - FLAGGED - message is "flagged" for urgent/special attention - RECENT - message is recent - SEEN - message has been read Opposites of the above flags: - UNANSWERED - UNDELETED - UNDRAFT - UNFLAGGED - OLD - UNSEEN Composite flags: - NEW - equivalent to RECENT + UNSEEN - ALL - all the messages | |
$selectedMailbox | string | Holds the currently selected mailbox. | |
$serverType | string | Used for fixing problems with Google IMAP (see issue #14360). Possible values are {@link self::SERVER_GIMAP} or null for all other servers. | |
$state | Holds the connection state. |
Method | Description | |
---|---|---|
__construct ( string $server, integer $port = null, ezcMailImapTransportOptions | array(string=>mixed) $options = [] ) | Creates a new IMAP transport and connects to the $server at $port. | |
__destruct ( ) | Destructs the IMAP transport. | |
__get ( string $name ) | Returns the value of the property $name. | |
__isset ( string $name ) : boolean | Returns true if the property $name is set, otherwise false. | |
__set ( string $name, mixed $value ) | Sets the value of the property $name to $value. | |
append ( string $mailbox, string $mail, array(string) $flags = null ) | Appends $mail to the $mailbox mailbox. | |
authenticate ( string $user, string $password ) : boolean | Authenticates the user to the IMAP server with $user and $password. | |
capability ( ) : array(string) | Returns an array with the capabilities of the IMAP server. | |
clearFlag ( string $messages, string $flag ) : boolean | Clears $flag from $messages. | |
copyMessages ( string $messages, string $destination ) : boolean | Copies message(s) from the currently selected mailbox to mailbox $destination. | |
countByFlag ( string $flag ) : integer | Wrapper function to fetch count of messages by a certain flag. | |
createMailbox ( string $mailbox ) : boolean | Creates the mailbox $mailbox. | |
delete ( integer $msgNum ) : boolean | Deletes the message with the message number $msgNum from the current mailbox. | |
deleteMailbox ( string $mailbox ) : boolean | Deletes the mailbox $mailbox. | |
disconnect ( ) | Disconnects the transport from the IMAP server. | |
expunge ( ) | Sends an EXPUNGE command to the server. | |
fetchAll ( boolean $deleteFromServer = false ) : ezcMailParserSet | Returns an {@link ezcMailImapSet} with all the messages from the current mailbox. | |
fetchByFlag ( string $flag ) : ezcMailImapSet | Returns an {@link ezcMailImapSet} containing messages with a certain flag from the current mailbox. | |
fetchByMessageNr ( integer $number, boolean $deleteFromServer = false ) : ezcMailImapSet | Returns an {@link ezcMailImapSet} containing only the $number -th message in the current mailbox. | |
fetchFlags ( array $messages ) : array(mixed) | Fetches IMAP flags for messages $messages. | |
fetchFromOffset ( integer $offset, integer $count, boolean $deleteFromServer = false ) : ezcMailImapSet | Returns an {@link ezcMailImapSet} with $count messages starting from $offset from the current mailbox. | |
fetchSizes ( array $messages ) : array(int) | Fetches the sizes in bytes for messages $messages. | |
getHierarchyDelimiter ( ) : string | Returns the hierarchy delimiter of the IMAP server, useful for handling nested IMAP folders. | |
listMailboxes ( string $reference = '', string $mailbox = '*' ) : array(string) | Returns an array with the names of the available mailboxes for the user currently authenticated on the IMAP server. | |
listMessages ( string $contentType = null ) : array(int) | Returns a list of the not deleted messages in the current mailbox. | |
listUniqueIdentifiers ( integer $msgNum = null ) : array(string) | Returns the unique identifiers for the messages from the current mailbox. | |
noop ( ) | Sends a NOOP command to the server, use it to keep the connection alive. | |
renameMailbox ( string $mailbox, string $newName ) : boolean | Renames the mailbox $mailbox to $newName. | |
searchMailbox ( string $criteria = null ) : ezcMailImapSet | Returns an {@link ezcMailImapSet} containing the messages which match the provided $criteria from the current mailbox. | |
selectMailbox ( string $mailbox, boolean $readOnly = false ) | Selects the mailbox $mailbox, which will be the active mailbox for the subsequent commands until it is changed. | |
setFlag ( string $messages, string $flag ) : boolean | Sets $flag on $messages. | |
sortFromOffset ( integer $offset, integer $count, string $sortCriteria, boolean $reverse = false ) : ezcMailImapSet | Returns an {@link ezcMailImapSet} containing $count messages starting from $offset sorted by $sortCriteria from the current mailbox. | |
sortMessages ( array(int) $messages, string $sortCriteria, boolean $reverse = false ) : ezcMailImapSet | Returns an {@link ezcMailImapSet} containing messages $messages sorted by $sortCriteria from the current mailbox. | |
status ( &$numMessages, &$sizeMessages, &$recent, &$unseen ) : boolean | Returns information about the messages in the current mailbox. | |
top ( integer $msgNum, integer $chars ) : string | Returns the headers and the first characters from message $msgNum, without setting the SEEN flag. |
Method | Description | |
---|---|---|
getMessageSectionSize ( $response ) : integer | Returns the size of a FETCH section in bytes. | |
getNextTag ( ) : string | Generates the next IMAP tag to prepend to client commands. | |
getResponse ( string $tag, string $response = null ) : string | Reads the responses from the server until encountering $tag. | |
normalizeFlag ( string $flag ) : string | Clears $flag of unwanted characters and makes it uppercase. | |
responseType ( string $line ) : integer | Parses $line to return the response code. | |
searchByFlag ( string $flag ) : array(int) | Returns an array of message numbers from the selected mailbox which have a certain flag set. | |
sort ( array(int) $messages, string $sortCriteria, boolean $reverse = false ) : array(string) | Sorts message numbers array $messages by the specified $sortCriteria. |
replace with your IMAP server address
$imap = new ezcMailImapTransport( 'imap.example.com' );
if you want to use SSL:
$options = new ezcMailImapTransportOptions();
$options->ssl = true;
$imap = new ezcMailImapTransport( 'imap.example.com', null, $options );
public __construct ( string $server, integer $port = null, ezcMailImapTransportOptions | array(string=>mixed) $options = [] ) | ||
$server | string | |
$port | integer | |
$options | ezcMailImapTransportOptions | array(string=>mixed) |
public __destruct ( ) |
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' );
array( 'IMAP4rev1', 'SASL-IR SORT', 'THREAD=REFERENCES', 'MULTIAPPEND',
'UNSELECT', 'LITERAL+', 'IDLE', 'CHILDREN', 'NAMESPACE',
'LOGIN-REFERRALS'
);
Before calling this method, a connection to the IMAP server must be
established. public capability ( ) : array(string) | ||
return | array(string) |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
$imap->clearFlag( '1:4', 'DRAFT' );
public countByFlag ( string $flag ) : integer | ||
$flag | string | |
return | integer |
public createMailbox ( string $mailbox ) : boolean | ||
$mailbox | string | |
return | boolean |
public deleteMailbox ( string $mailbox ) : boolean | ||
$mailbox | string | |
return | boolean |
public expunge ( ) |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'Inbox' );
$set = $imap->fetchAll();
parse $set with ezcMailParser
$parser = new ezcMailParser();
$mails = $parser->parseMail( $set );
foreach ( $mails as $mail )
{
process $mail which is an ezcMail object
}
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
Fetch the messages marked with the RECENT flag
$set = $imap->fetchByFlag( 'RECENT' );
$set can be parsed with ezcMailParser
public fetchByFlag ( string $flag ) : ezcMailImapSet | ||
$flag | string | |
return | ezcMailImapSet |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'Inbox' );
$set = $imap->fetchByMessageNr( 1 );
$set can be parsed with ezcMailParser
public fetchByMessageNr ( integer $number, boolean $deleteFromServer = false ) : ezcMailImapSet | ||
$number | integer | |
$deleteFromServer | boolean | |
return | ezcMailImapSet |
array( 1, 2, 4 );
The format of the returned array is:
array( message_number => array( flags ) )
Before calling this method, a connection to the IMAP server must be
established and a user must be authenticated successfully, and a mailbox
must be selected.
Example:
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
$flags = $imap->fetchFlags( array( 1, 2, 4 ) );
The returned array $flags will be something like:
array( 1 => array( '\Seen' ),
2 => array( '\Seen' ),
4 => array( '\Seen', 'NonJunk' )
);
public fetchFlags ( array $messages ) : array(mixed) | ||
$messages | array | |
return | array(mixed) |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'Inbox' );
$set = $imap->fetchFromOffset( 1, 10 );
$set can be parsed with ezcMailParser
array( 1, 2, 4 );
The format of the returned array is:
array( message_number => size )
Before calling this method, a connection to the IMAP server must be
established and a user must be authenticated successfully, and a mailbox
must be selected.
Example:
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
$sizes = $imap->fetchSizes( array( 1, 2, 4 ) );
The returned array $sizes will be something like:
array( 1 => 1043,
2 => 203901,
4 => 14277
);
public fetchSizes ( array $messages ) : array(int) | ||
$messages | array | |
return | array(int) |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$delimiter = $imap->getDelimiter();
After running the above code, $delimiter should be something like "/". public getHierarchyDelimiter ( ) : string | ||
return | string |
protected getMessageSectionSize ( $response ) : integer | ||
return | integer |
protected getNextTag ( ) : string | ||
return | string |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$mailboxes = $imap->listMailboxes();
public listMailboxes ( string $reference = '', string $mailbox = '*' ) : array(string) | ||
$reference | string | |
$mailbox | string | |
return | array(string) |
array( message_id => size );
Example:
array( 2 => 1700, 5 => 1450, 6 => 21043 );
If $contentType is set, it returns only the messages with
$contentType in the Content-Type header.
For example $contentType can be "multipart/mixed" to return only the
messages with attachments. public listMessages ( string $contentType = null ) : array(int) | ||
$contentType | string | |
return | array(int) |
array( message_num => unique_id );
Example:
array( 1 => 216, 2 => 217, 3 => 218, 4 => 219 );
Before calling this method, a connection to the IMAP server must be
established and a user must be authenticated successfully, and a mailbox
must be selected. public listUniqueIdentifiers ( integer $msgNum = null ) : array(string) | ||
$msgNum | integer | |
return | array(string) |
public noop ( ) |
protected normalizeFlag ( string $flag ) : string | ||
$flag | string | |
return | string |
protected responseType ( string $line ) : integer | ||
$line | string | |
return | integer |
array( 0 => 1, 1 => 5 );
Before calling this method, a connection to the IMAP server must be
established and a user must be authenticated successfully, and a mailbox
must be selected. protected searchByFlag ( string $flag ) : array(int) | ||
$flag | string | |
return | array(int) |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
return an ezcMailImapSet containing all messages flagged as 'SEEN'
$set = $imap->searchMailbox( 'SEEN' );
return an ezcMailImapSet containing messages with 'release' in their Subject
$set = $imap->searchMailbox( 'SUBJECT "release"' );
criterias can be combined:
return an ezcMailImapSet containing messages flagged as 'SEEN' and
with 'release' in their Subject
$set = $imap->searchMailbox( 'SEEN SUBJECT "release"' );
$set can be parsed with ezcMailParser
public searchMailbox ( string $criteria = null ) : ezcMailImapSet | ||
$criteria | string | |
return | ezcMailImapSet |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'Reports 2006' );
public selectMailbox ( string $mailbox, boolean $readOnly = false ) | ||
$mailbox | string | |
$readOnly | boolean |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
$imap->setFlag( '1:4', 'DRAFT' );
array( 1, 2, 4 );
$sortCriteria is an email header like: Subject, To, From, Date, Sender.
The sorting is done with the php function natcasesort().
Before calling this method, a connection to the IMAP server must be
established and a user must be authenticated successfully, and a mailbox
must be selected.
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
Fetch a range of messages sorted by Date
$set = $imap->sortFromOffset( 1, 10, "Date" );
$set can be parsed with ezcMailParser
array( 1, 2, 4 );
$sortCriteria is an email header like: Subject, To, From, Date, Sender, etc.
Before calling this method, a connection to the IMAP server must be
established and a user must be authenticated successfully, and a mailbox
must be selected.
Example:
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox
Fetch the list of messages sorted by Date
$set = $imap->sortMessages( 1, 10, "Date" );
$set can be parsed with ezcMailParser
public sortMessages ( array(int) $messages, string $sortCriteria, boolean $reverse = false ) : ezcMailImapSet | ||
$messages | array(int) | |
$sortCriteria | string | |
$reverse | boolean | |
return | ezcMailImapSet |
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'Inbox' );
$imap->status( $numMessages, $sizeMessages, $recent, $unseen );
After running the above code, $numMessages, $sizeMessages, $recent
and $unseen will be populated with values.
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'Inbox' );
$parser = new ezcMailParser();
$messages = $imap->listMessages();
foreach ( $messages as $messageNr => $size )
{
$set = new ezcMailVariableSet( $imap->top( $messageNr ) );
$mail = $parser->parseMail( $set );
$mail = $mail[0];
echo "From: {$mail->from}, Subject: {$mail->subject}, Size: {$size}\n";
}
For a more advanced example see the "Mail listing example" in the online
documentation. protected static array(string) $basicFlags | ||
return | array(string) |
protected ezcMailTransportConnection $connection | ||
return | ezcMailTransportConnection |
protected string $currentTag | ||
return | string |
protected static array(string) $extendedFlags | ||
return | array(string) |
protected string $selectedMailbox | ||
return | string |
protected string $serverType | ||
return | string |