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.
Download ssilence/php-imap-client
More information about ssilence/php-imap-client
Files in ssilence/php-imap-client
Package php-imap-client
Short Description Php imap client
License MIT
Homepage https://github.com/SSilence/php-imap-client
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:
- simple interface
- Get emails and folders
- Move, delete, count emails
- Rename, delete and
- Get attachments
- Many more
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
- Getting started
- Connecting
- Errors
- Installing
- Methods
- Usage
- Examples
- Incoming Message
- Sending Emails
- 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 |
|
| File must be specified for saveEmail() | You did not specify a file path | Insure your code looks like this: |
| Email id must be specified for saveEmail() | You did not specify an email id | Insure your code looks like this: |
| 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
ext-imap Version *