Constants

ENCRYPT_SSL

ENCRYPT_SSL

Use the Secure Socket Layer to encrypt the session

ENCRYPT_TLS

ENCRYPT_TLS

Force use of start-TLS to encrypt the session, and reject connection to servers that do not support it

CONNECT_ADVANCED

CONNECT_ADVANCED

CONNECT_DEFAULT

CONNECT_DEFAULT

Properties

$connect

$connect : string

Connect status or advanced or default

Type

string

$connectConfig

$connectConfig : array

Config for advanced connect

Type

array

$imap

$imap : resource

Imap connection

Type

resource — ImapConnect

$mailbox

$mailbox : string

Mailbox url

Type

string

$folder

$folder : string

Current folder

Type

string

Methods

__construct()

__construct(string  $mailbox = null, string  $username = null, string  $password = null, string  $encryption = null) 

Initialize imap helper

Parameters

string $mailbox
string $username
string $password
string $encryption

use ImapClient::ENCRYPT_SSL or ImapClient::ENCRYPT_TLS

setConnectAdvanced()

setConnectAdvanced() : void

Set connection to advanced

setConnectDefault()

setConnectDefault() : void

Set connection to default

setConnectConfig()

setConnectConfig(array  $config) : void

Set connection config

Parameters

array $config

connectDefault()

connectDefault(string  $mailbox, string  $username, string  $password, string  $encryption = null) : void

The default connection.

Not used a lot of imap connection options. Use only ENCRYPT_SSL and VALIDATE_CERT.

If you need a more advanced connection settings, use connectAdvanced() method.

Parameters

string $mailbox
string $username
string $password
string $encryption

use ImapClient::ENCRYPT_SSL or ImapClient::ENCRYPT_TLS

connectAdvanced()

connectAdvanced(array  $config) : void

Advanced connect

Parameters

array $config

Throws

\SSilence\ImapClient\ImapClientException

__destruct()

__destruct() : void

Close connection

Also called during garbage collection

isConnected()

isConnected() : boolean

Returns true after successful connection

Returns

boolean —

true on success

saveEmail()

saveEmail(string  $file = null, integer  $id = null, string  $part = null) : boolean

Saves the email into a file Note: If your server does not have alot of RAM, this may break

Parameters

string $file
integer $id
string $part

Throws

\SSilence\ImapClient\ImapClientException

Returns

boolean —

true on success

saveEmailSafe()

saveEmailSafe(string  $file = null, integer  $id = null, string  $part = null, string  $streamFilter = 'convert.base64-decode') : boolean

Saves the email into a file Note: This is safer then saveEmail for slower servers

Parameters

string $file
integer $id
string $part
string $streamFilter

Throws

\SSilence\ImapClient\ImapClientException

Returns

boolean —

true on success

getError()

getError() : string

Returns the last imap error

Returns

string —

error message

selectFolder()

selectFolder(string  $folder) : boolean

Select the given folder folder and set $this->folder

Parameters

string $folder

name

Returns

boolean —

successful opened folder

getFolders()

getFolders(string  $separator = null, integer  $type) : array

Returns all available folders

Parameters

string $separator

default is '.'

integer $type

has three meanings 0,1,2. If 0 return nested array, if 1 return an array of strings, if 2 return raw imap_list()

Returns

array —

with folder names

countMessages()

countMessages() : integer

Returns the number of messages in the current folder

Returns

integer —

message count

getBriefInfoMessages()

getBriefInfoMessages() : array

Returns an array of brief information about each message in the current mailbox.

Returns the following structure of the array of arrays.

$array = [
    [ 'id'=>4, 'info'=>'brief info' ]
    [ 'id'=>5, 'info'=>'brief info' ]
]

Returns

array

countUnreadMessages()

countUnreadMessages() : integer

Returns the number of unread messages in the current folder

Returns

integer —

Message count

getUnreadMessages()

getUnreadMessages(boolean  $read = true) : array

Returns unseen emails in the current folder

Parameters

boolean $read

Mark message like SEEN or no.

Throws

\SSilence\ImapClient\ImapClientException

Returns

array —

objects IncomingMessage

getMessagesByCriteria()

getMessagesByCriteria(string  $criteria = '', integer  $number, integer  $start, string  $order = 'DESC') : array

Get Messages by Criteria

Parameters

string $criteria

ALL, UNSEEN, FLAGGED, UNANSWERED, DELETED, UNDELETED, etc (e.g. FROM "joey smith")

integer $number
integer $start
string $order

Throws

\SSilence\ImapClient\ImapClientException

Returns

array

saveAttachmentsMessagesBySubject()

saveAttachmentsMessagesBySubject(string  $subject, string  $dir = null, string  $charset = null) : void

Save Attachments Messages By Subject

Parameters

string $subject
string $dir

for save attachments

string $charset

for search

Throws

\SSilence\ImapClient\ImapClientException

getMessages()

getMessages(integer  $number, integer  $start, string  $order = 'DESC') : array

Get messages

Parameters

integer $number

Number of messages. 0 to get all

integer $start

Starting message number

string $order

ASC or DESC

Returns

array —

IncomingMessage of objects

getMessage()

getMessage(integer  $id, string  $decode = \SSilence\ImapClient\IncomingMessage::DECODE) : object

Returns one email by given id

Examples:

  1. Structure

    $imap = new ImapClient();
    $imap->getMessage(5);

    You can see all structure that

    var_dump($imap->incomingMessage)

    But use like this

    $imap->incomingMessage->header->subject
    $imap->incomingMessage->header->from
    $imap->incomingMessage->header->to
    # cc or bcc
    $imap->incomingMessage->header->details->cc
    $imap->incomingMessage->header->details->bcc
    # and other ...
    var_dump($imap->incomingMessage->header)

    Next Text or Html body

    $imap->incomingMessage->message->html
    $imap->incomingMessage->message->plain
    # below is array
    $imap->incomingMessage->message->info

    Array attachments

    $imap->incomingMessage->attachment

    Attachment have structure and body

    $imap->incomingMessage->attachment[0]
    $imap->incomingMessage->attachment[0]->structure
    $imap->incomingMessage->attachment[0]->body

    Count section

    $imap->incomingMessage->section

    And structure all message

    $imap->incomingMessage->structure
  2. Save all attachments
    $imap->getMessage(5);
    $imap->saveAttachments();

Parameters

integer $id
string $decode

IncomingMessage::DECODE or IncomingMessage::NOT_DECODE

Returns

object —

IncomingMessage

getSection()

getSection(integer  $id, string  $section) : object

Get a section of the message

Parameters

integer $id
string $section

Returns

object

saveAttachments()

saveAttachments(array  $options = null) : void

Save attachments one incoming message

The allowed types are TypeAttachments You can add your own

Parameters

array $options

have next parameters

# it is directory for save attachments
$options['dir']
# it is incomingMessage object
$options['incomingMessage']

deleteMessage()

deleteMessage(integer  $id) : boolean

Delete the given message

Parameters

integer $id

of the message

Returns

boolean —

success or not

deleteMessages()

deleteMessages(  $ids) : boolean

Delete messages

Parameters

$ids

array of ids

Returns

boolean —

success or not

moveMessage()

moveMessage(integer  $id, string  $target) : boolean

Move given message in new folder

Parameters

integer $id

of the message

string $target

new folder

Returns

boolean —

success or not

moveMessages()

moveMessages(array  $ids, string  $target) : boolean

Move given message in new folder

Parameters

array $ids

array of message ids

string $target

new folder

Returns

boolean —

success or not

setUnseenMessage()

setUnseenMessage(integer  $ids) : boolean

Delete flag message SEEN

Parameters

integer $ids

or string like 1,2,3,4,5 or string like 1:5

Returns

boolean

addFolder()

addFolder(string  $name, boolean|false  $subscribe = false) : boolean

Add a new folder

Parameters

string $name

of the folder

boolean|false $subscribe

immediately subscribe to folder

Returns

boolean —

success or not

removeFolder()

removeFolder(string  $name) : boolean

Remove a folder

Parameters

string $name

of the folder

Returns

boolean —

success or not

renameFolder()

renameFolder(string  $name, string  $newname) : boolean

Rename a folder

Parameters

string $name

of the folder

string $newname

of the folder

Returns

boolean —

success or not

purge()

purge() : boolean

Clean up trash AND spam folder

Returns

boolean —

success or not

getAllEmailAddresses()

getAllEmailAddresses(array|null  $options = null) : array

Returns all email addresses in all folders

If you have a lot of folders and letters, it can take a long time. And mark all the letters as read.

Parameters

array|null $options

have options:

$options['getFolders']['separator']
$options['getFolders']['type']
$options['mark'] = SEEN and $options['mark'] = UNSEEN

Returns

array —

with all email addresses or false on error

getEmailAddressesInFolder()

getEmailAddressesInFolder(string  $folder, array|null  $options = null) : array

Returns email addresses in the specified folder

Parameters

string $folder

Specified folder

array|null $options

have option

$options['mark'] = SEEN and $options['mark'] = UNSEEN

Returns

array —

addresses

saveMessageInSent()

saveMessageInSent(string  $header, string  $body) : boolean

Save email in sent

Parameters

string $header
string $body

Returns

boolean

close()

close() 

Explicitly close imap connection

getMessageOverview()

getMessageOverview(integer  $id, null  $options = null) : object

Get message overview

Parameters

integer $id
null $options

Returns

object

getMessagesOverview()

getMessagesOverview(string  $id, null  $options = null) : array

Get messages overview

Parameters

string $id
null $options

Returns

array

imapFetchOverview()

imapFetchOverview(string  $sequence, integer  $options = null) : array

Wrapper for php imap_fetch_overview()

Parameters

string $sequence

a message sequence description, you can enumerate desired messages with the X,Y syntax, or retrieve all messages within an interval with the X:Y syntax

integer $options

sequence will contain a sequence of message indices or UIDs, if this parameter is set to FT_UID.

Returns

array

imapHeaderInfo()

imapHeaderInfo(integer  $id) : object|false

Wrapper for php imap_headerinfo()

Parameters

integer $id

Returns

object|false

imapFetchStructure()

imapFetchStructure(integer  $id) : object

Wrapper for imap_fetchstructure()

Parameters

integer $id

Returns

object

convertToUtf8()

convertToUtf8(string  $str) : boolean

Convert to utf8 if necessary.

Parameters

string $str

utf8 encoded string

Returns

boolean

getMailboxStatistics()

getMailboxStatistics() : boolean|resource

Return general mailbox statistics

Returns

boolean|resource —

object

unSubscribe()

unSubscribe() : boolean

Unsubscribe from a mail box

Returns

boolean

getQuota()

getQuota(string  $mailbox) : array

Retrieve the quota level settings, and usage statics per mailbox.

Parameters

string $mailbox

Returns

array

getQuotaRoot()

getQuotaRoot(string  $mailbox) : array

Retrieve the quota level settings, and usage statics per mailbox.

Parameters

string $mailbox

Returns

array

getUid()

getUid(integer  $id) : integer

Get uid from id

Parameters

integer $id

Returns

integer

getId()

getId(integer  $uid) : integer

Get id from uid

Parameters

integer $uid

Returns

integer

sendMail()

sendMail() : void

Send an email

getTrash()

getTrash() : string

Get trash folder

Returns

string —

trash folder name

getSent()

getSent() : string

Get sent

Returns

string —

sent folder name

getMessageHeader()

getMessageHeader(integer  $id) : object|false

Fetch message by id

Parameters

integer $id

of the message

Returns

object|false —

header

toAddress()

toAddress(object  $headerinfos) : string

Convert imap given address into string

Parameters

object $headerinfos

the infos given by imap

Returns

string —

in format "Name email@bla.de"

arrayToAddress()

arrayToAddress(array  $addresses) : array

Converts imap given array of addresses as strings

Parameters

array $addresses

imap given addresses as array

Returns

array —

with strings (e.g. ["Name email@bla.de", "Name2 email2@bla.de"]

getEncoding()

getEncoding(  $id) : string

Identify encoding by charset attribute in header

Parameters

$id

Returns

string

checkMessageId()

checkMessageId(integer  $id) : void

Check message id

Parameters

integer $id

Throws

\SSilence\ImapClient\ImapClientException