Download the PHP package ssilence/php-imap-client without Composer

On this page you can find all versions of the php package ssilence/php-imap-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-imap-client

Welcome to php-imap-client

Hello! This is php-imap-client. This is a simple and easy to use library for connecting to imap servers and working with the emails inside. Our well-known features are:

Click to open documentation

[!CAUTION] php-imap-client base on the IMAP extensions. From PHP 8.4 it will move from PHP core to PECL. The IMAP extension can still be installed from PECL but the Webklex/php-imap library doesn't need any extension.

Documentation

  1. Getting started
  2. Connecting
  3. Errors
  4. Installing
  5. Methods
  6. Usage
  7. Examples
  8. Incoming Message
  9. Sending Emails
  10. License

Getting started

In order to get started, we first need to get php-imap-client to your code base.
There are currently two ways of doing this.

1) Composer

composer require ssilence/php-imap-client:^3.0

2) Manually

1) Download the files from github or the releases page
2) Extract the files into the folder you wish
3) In the file that will call methods add

After this, we need to let php we need these classes:

Next, we need to declare out variables:

Next, we need to open the connection:

After that we can do all this fun stuff :)

Connecting

Default connnection

A default connection is the easy and most uncommon way to connect to an imap sever. Use the code below to use our default connection method.

This code does not check for errors and assumeing everything is right. If you are using this in production be sure to check for errors

Advanced Connecting

We also have ways to change how your connection works. Read the code and examples below to learn some ways to modifiy your connection,

Errors

Many errors may be thrown while using the library, if you cant seem to find what an error means or what you are doing wrong, take a look here. Everything is structured like this:

Error Description Solution/Note
Imap function not available PHP does not support connections to web servers via imap To fix this download the php_imap.dll and enable it by putting the following text in php.ini extension=php_gettext.dll
Mailbox is not installed No mail box was provided Confirm that the $mailbox variable is filled when connecting
Mailbox must be a string The mailbox provided to connect to the web server is not a string We cannot connect to mail boxes that have a integer in them. Make sure the $mailbox variable provied is a string
Username must be a string The username provided to connect to the web server is not a string Web servers dont use arrays as usernames!!! Make sure the $username variable is a string
Password must be a string The password provided to connect to the web server is not a string Confirm that the $password variable is a string
Options must be an integer The options variable provided when connecting is not an integer // Dont know. Someone checking pr tell me and ill fix
N_ must be an integer The number of retries provided is not an integer Make sure that the $N_retries variable is an integer
Params must be an array The parameters provided to connect to the server are not an array // Dont know. Someone checking pr tell me and ill fix
Error connecting to [insert your mailbox string here] PHP-imap client had trouble connecting to the provided mailbox with the provided details This can mean many things. It can mean your mailbox is invalid or your username and password are not valid. Comfirm your login details and make sure your mail server is online
Option connect must be installed If you selected the advanced connection and not installed connect option like
$imap = new ImapClient([
'connect' => [
'username' => 'user',
'password' => 'pass',
]
]);
File must be specified for saveEmail() You did not specify a file path Insure your code looks like this:
$imap->saveEmail($your_file_path_var, $your_email_id_var, $your_part_var)
Email id must be specified for saveEmail() You did not specify an email id Insure your code looks like this:
$imap->saveEmail($your_file_path_var, $your_email_id_var, $your_part_var)
File must be a string for saveEmail() The provided file path is not a string Make sure your $file is a string not a open file
$id must be a integer for saveEmail() The provided email id is an integer Make sure your $id is an integer
What to use id or uid? You did not let us know weather to use id or uuids

Installing

PHP-imap-client can be installed 2 ways. The first composer and the second manual

1) Composer

Use the following command to install php-imap-client library: composer require ssilence/php-imap-client:^3.0

2) Manual

1) Download the files from github or the releases page
2) Extract the files into the folder you wish
3) In the file that will call methods add

You may then use connect etc

Methods

The following methods are currently available.

Method Description
__construct($mailbox, $username, $password, $encryption) open new imap connection
isConnected() check whether the imap connection was opened successfully
getError() returns the last error message.
getFolders($separator, $type) @param string $separator. Default is '.' @param int $type. Has three meanings 0,1,2. If 0 returns a nested array, if 1 it returns an array of strings, if 2 returns raw data from imap_list().
getMessage($id) get email by given id.
getMessages($number, $start, $order) get emails in current folder.
getUnreadMessages($read) get unread messages in current folder and mark them read. Use $read = false marks them unread.
getQuota($user) Retrieve the quota level settings, and usage statics per mailbox.
getQuotaRoot($user) Retrieve the quota level settings, and usage statics per mailbox.
getAllEmailAddresses() returns all email addresses of all emails (for auto suggestion list).
getMailboxStatistics() returns statistics, see imap_mailboxmsginfo.
getHeaderInfo($msgNumber) Get the header info via the message number. https://php.net/manual/en/function.imap-headerinfo.php#refsect1-function.imap-headerinfo-returnvalues
getMessagesByCriteria($criteria, $number, $start, $order) Get messages by criteria like 'FROM uncle'.
getBriefInfoMessages() Get a short information about the messages in the current folder.
getSection($id, $section) Get the section of the specified message.
getUid($id) Get uid through id.
getId($uid) Get id through uid.
setUnseenMessage($id, $seen = true) set unseen state of the message with given id.
setEmbed($val) If true, embed all 'inline' images into body HTML, accesible in 'body_embed'.
setEncoding() Identify encoding by charset attribute in header.
selectFolder($folder) select the provided folder.
countMessages() count the messages in current folder.
countUnreadMessages() count the unread messages in current folder.
deleteMessage($id) delete message with given id.
deleteMessages($ids) delete messages with given ids (as array).
moveMessage($id, $target) move message with given id in new folder
moveMessages($ids, $target) move messages with given ids (as array) in new folder
addFolder($name) add new folder with given name
removeFolder($name) delete folder with given name
renameFolder($name, $newname) rename folder with given name
purge() move all emails in the current folder into trash. emails in trash and spam will be deleted.
convertToUtf8() Apply encoding defined in header
saveMessageInSent($header, $body) save a sent message in sent folder
saveEmail($file , $id, $part) saves an email to the $file file
saveEmailSafe($file , $id, $part, $streamFilter) saves an email to the $file file. This is recommended for servers with low amounts of RAM. Stream filter is set to convert.base64-decode by default.
saveAttachments($options) | Save attachments one incoming message. You can set any of the options: $options['dir'=>null, 'incomingMessage'=>null].
saveAttachmentsMessagesBySubject($subject, $dir = null, $charset = null) Save Attachmets Messages By Subject
sendMail() Send a message using the adapter.

Usage

After install prep

After you install this library ensure you have added the required classes. A basic connection may look like this:

The above code connects you to a mail server and makes sure it connected. Change the variables to your information

After connection

There are many things you can do after the code above. For example you can get and echo all folders

See getFolders() method settings. You can also select folders

Once you selected a folder you can count the number of messages in the folder:

To get a brief summary of all messages in the current folder, including the message ID you can use this

Get the message with ID 82

Save all of the attachmets in this email.

or

or save the attachment(s) like this

It is also possible to save all attachments for messages with a special word in the message subject.

Get the header info like cc and bcc

Get all unread messages.

Okay, now lets fetch all emails in the currently selected folder (in our example the "Inbox"):

Now $emails it is array object.

The structure of a single message when it is received by the method getMessage() or getMessages() it by the look here Incoming Message

For example get subject and simple text messages

You can also add/rename/delete folders. Lets add a new folder:

Now we move the first email into this folder

And we delete the second email from inbox

We also can save emails

You can use the method of sending messages.

But for this you need to take several steps. Adapter for outgoing message. Use in 3 steps.

For a full list of methods you can do check current list of methods.

Advanced connecting

You can also use the below code to add some more options while connecting

All connecting options you can see in example-connect.php file or go Advanced connecting or you can see code ImapConnect class.

Examples

Composer

Connect

Direct

Incoming Message

Structure

Incoming Message is an object(SSilence\ImapClient\IncomingMessage) that has 5 basic properties

and one for debugging

Header properties

The header properties typically look like this:

To get the subject of the message, use $imap->incomingMessage->header->subject

This object also contains the id and the uid of the current message according to https://php.net/manual/en/function.imap-fetch-overview.php:

To get the CC or BCC use the property

Header details properties have more properties like this imap_headerinfo. If there is no property in the returned object, then it is not present in this email. To get the CC or BCC of the message, use

Message properties

Message properties will look like this:

To get info on the message use $imap->incomingMessage->message->info Is an array. It will allways return an array

To get the body of the message use

Get the emails charset like this:

Get all subtype message:

These are auto generated by the client. This allows you to have an html and a plain text version of the email

Attachments properties

Attachment properties is an array of objects. If the letter has attachments, then its return will look like as follows

The attachment object has 3 basic properties. Like this:

To get the names of all attachments in an email, use the following

All information for the email will be in the info property. It will look like this:

The attachment info object has 2 basic properties.

Body properties contain the body attachment(s).

Section properties

Section properties it is array sections of which the email consists.

Structure properties

The structure propertie is the complete structure of the email

Debug properties

The debug propertie can be used for debugging

Sending Emails

The IMAP protocol is designed to read messages and does not support the sending of messages. But if you want ImapClient to send messages, then use the AdapterForOutgoingMessage class. To do this you will also need to install PHPMailer

First step

The first step is to install php mailer. You can do so in two ways.

1) Composer

2) Minimal Read PHPMailer's guide on a minimal installation

Second step

Turn phpmailer into an adapter. File AdapterForOutgoingMessage.php To do this, we rewrite send() method. Note this is note safe yet and may be re written in the future.

Third step

Now you can send messages like this:

License

Copyright (c) 2025 Tobias Zeising [email protected]
https://www.aditu.de
Licensed under the MIT license


All versions of php-imap-client with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-imap Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ssilence/php-imap-client contains the following files

Loading the files please wait ...