Back to the module list

IMAP support

The IMAP (Internet Message Access Protocol) support allow to interact from PHP with a IMAP server. It can read, after authentication, the user mailbox and drive it.

The server can be secured by SSL. Then the IMAPS protocol is used, with a parameter to check or not the server certificate.

It can list, read the mails, move or delete one or multiple ones. It manage the folders (list, add, delete, subscribe, unsubscribe). It is also capable to read the quota state and define the mail flags.

The class definition

Class \imap

Namespace \

Description

 IMAP connection abstraction
    In the IMAP terminology, "mailbox" is a folder in the mailbox of the user

Properties

public $autoexpunge=true;
 The auto expunge feature, after deleting/moving an email

Methods

public function __construct ($imapserver="localhost", $imapport=143, $username=null, $password=null, $imapssl=false, $imapcertvalidate=true)
 The constructor
 The IMAP standard port is 143, but SSL tunnelled is 993
 @param string|null $imapserver The IMAP server to connect. Localhost is
 used if not defined
 @param integer|null $imapport The IMAP port to connect. 143 is used if not
 defined
 @param string|null $username The username to connect
 @param string|null $password The password to connect
 @param boolean|null $imapssl Use the SSL connection layer. Not used by
 default
 @param boolean|null $imapcertvalidate Check the certificates if using the
 SSL connection. True by default

public function addSubscribe ($folder)
 Add a subscription for a folder. The folder must be in UTF-8
 @param string $folder Add the provided folder to the subscription file of
 the user

public function changeFolder ($folder)
 Change to provided folder
 The folder name must be in UTF-8. The folder must be absolute
 @param string $folder Change to the provided folder

public function connect ($imapserver="localhost", $imapport=143, $username=null, $password=null, $imapssl=false, $imapcertvalidate=true)
 The connect can be used when extends the imap class. The constructor can
 be override by the child class.
 The IMAP standard port is 143, but SSL tunnelled is 993
 @param string|null $imapserver The IMAP server to connect. Localhost is
 used if not defined
 @param integer|null $imapport The IMAP port to connect. 143 is used if not
 defined
 @param string|null $username The username to connect
 @param string|null $password The password to connect
 @param boolean|null $imapssl Use the SSL connection layer. Not used by
 default
 @param boolean|null $imapcertvalidate Check the certificates if using the
 SSL connection. True by default

public function createFolder ($folder)
 Create a new folder, provided in UTF-8. The folder must be absolute
 @param string $folder Create the provided folder

public function delSubscribe ($folder)
 Remove a subscription for a folder. The folder must be in UTF-8
 @param string $folder Remove the provided folder to the subscription file
 of the user

public function deleteFolder ($folder)
 Delete an existing folder provided in UTF-8. The folder must be absolute
 @param string $folder The folder to delete

public function expunge ()
 Expunge the mailbox. If the autoexpunge is activated, it is normally not
 needed

public function foldersList ()
 Return an array of the existing folders. The sub-folders are with slash
 separator
 The names of folders are converted in UTF-8

public function foldersListWithAttr ()
 Return an array with folder name in key and attributes in value. The
 attributes allow to see if there is new mails in folders

public function getEmailBodyRaw ($msgno)
 Get all the body (and attached files) of an email in raw format
 @param integer $msgno The message number to examine

public function getEmailHeadersRaw ($msgno)
 Get the headers of the email (in raw format)
 @param integer $msgno The message number to examine

public function getEmailRaw ($msgno)
 Get an existing email in the current folder in raw format
 @param integer $msgno The message number to examine

public function getFolder ()
 Return the current folder in UTF-8

public function getFolderInfo ($folder)
 Return the information concerning a folder. It return an object with the
 following properties :
   Date     date of last change (current datetime)
   Driver   driver
   Mailbox  name of the mailbox
   Nmsgs    number of messages
   Recent   number of recent messages
   Unread   number of unread messages
   Deleted  number of deleted messages
   Size     mailbox size
 @param string $folder The folder to get the informations

public function getQuota ()
 Return the quota used by the user in Mo

public function getStructure ($msgno)
 Return email structure of the body
 @param integer $msgno The message number to examine

public function getStructureContent ($msgno, $part)
 Return the content of a part of the mail body defined in the structure in
 an object, with the associated mimetype, the parameters like the charset
 if they are defined, the number of lines associated to this part in the
 mail and some other info
 @param integer $msgno The message number to examine
 @param integer $part The message part to get

public function getStructureParts ($msgno)
 Return the part identifiers of the structure of the mail body. To be used
 in getStructureContent
 @param integer $msgno The message number to examine

public function getStructureWithContent ($msgno)
 Return the structure of the mail body with the associated content
 @param integer $msgno The message number to examine

public function getSubscribe ()
 Return the list of the folders substcribed by the user. The folders are
 in UTF-8

public function imapSortMail ($mailHeaders, $field, $orderAsc=true)
 Return an array of mailHeaders order by $field and by order ASC or DESC
 @param array $mailHeaders The headers to sort
 @param string $field The field to examine
 @param boolean|null $orderAsc The order of sorting. Asc if not defined

public function mailAdd ($content)
 Add a new mail in the current folder. The content must be a string
 containing all the mail (header and body). If the content is invalid, the
 directory listing can provide erroneous data
 @param string $content the content of the mail to add

public function mailCopy ($msgno, $folder)
 Copy the mail provided in the $folder in UTF-8.
 If $msgno is an array, all the mails with the contain msgno are copied
 @param integer|array $msgno The message number(s) to copy
 @param string $folder The destination folder of the copy. Must exists

public function mailMove ($msgno, $folder)
 Move the mail provided in the $folder in UTF-8.
 If $msgno is an array, all the mails with the contain msgno are deleted
 Expunge automatically the current folder to remove the old emails
 @param integer|array $msgno The message number(s) to copy
 @param string $folder The destination folder of the move. Must exists

public function mailsDate ($from=1, $nbmails=30)
 Fetch the headers for all messages in the current folder sorted by date
 Return an array of mail object containing information like the subject,
 the date, if the message is already read (recent), answered...
 (see http://www.php.net/manual/en/function.imap-fetch-overview.php)
 If the $from is negative, take the LAST $from mails
 If from is zero, it's value is override to 1
 For information, takes 0.4s to select 30 mails on 1552
 @param integer|null $from The selector of the mails. 1 if not defined
 @param integer|null $nbmails The number of mails returned by the method
 30 if not defined

public function mailsDel ($msgno)
 Delete all the mailIDs (msgno) provided in an array or a single mail if
 $msgno is not an array
 DO NOT MOVE THE MAIL IN TRASH, DESTROY THE MAIL REALLY
 Expunge the mails at the end of the operation
 @param array|integer $msgno The message number(s) to remove

public function mailsNumber ()
 Send back the number of mails in the mailbox

public function mailsSearch ($criteria)
 Return an array containing the msgno corresponding to the criteria
 @param string $criteria The criteria to use for the IMAP search

public function mailsThread ()
 Return all the mails numbers order by thread in an array.
 [] => array ("msgno"=>msgno, "depth"=>depth)

public function markMailAsRead ($msgno)
 Mark mail(s) as read.
 If msgno is an array, a list of mails will be modified.
 If msgno is an integer, only one mail will be modified
 @param integer|array $msgno The messages number(s) to mark as read

public function markMailAsUnread ($msgno)
 Mark mail(s) as unread.
 If msgno is an array, a list of mails will be modified.
 If msgno is an integer, only one mail will be modified
 @param integer|array $msgno The messages number(s) to mark as unread

public function setFlag ($msgno, $flags)
 Set the flags of the msgno. If msgno is an array, the flags will be write
 on the list of mails. The others flags of the email are not modified.
 The flags must be an array containing :
 \Seen       Message has been read
 \Answered   Message has been answered
 \Flagged    Message is "flagged" for urgent/special attention
 \Deleted    Message is "deleted" for removal by later EXPUNGE
 \Draft      Message has not completed composition (marked as a draft).
 @param integer|array $msgno The messages number(s) to add the flags
 @param array $flags The flags to add

public function unsetFlag ($msgno, $flags)
 Unset the flags of the msgno. If msgno is an array, the flags will be
 write on the list of mails. The others flags of the email are not
 modified.
 The flags must be an array containing :
 \Seen       Message has been read
 \Answered   Message has been answered
 \Flagged    Message is "flagged" for urgent/special attention
 \Deleted    Message is "deleted" for removal by later EXPUNGE
 \Draft      Message has not completed composition (marked as a draft).
 @param integer|array $msgno The messages number(s) to remove the flags
 @param array $flags The flags to remove