Download the PHP package smartilabs/php-imap without Composer
On this page you can find all versions of the php package smartilabs/php-imap. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download smartilabs/php-imap
More information about smartilabs/php-imap
Files in smartilabs/php-imap
Package php-imap
Short Description PHP IMAP client
License MIT
Homepage https://github.com/webklex/php-imap
Informations about the package php-imap
IMAP Library for PHP
Description
PHP-IMAP is a wrapper for common IMAP communication without the need to have the php-imap module installed / enabled.
The protocol is completely integrated and therefore supports IMAP IDLE operation and the "new" oAuth authentication
process as well.
You can enable the php-imap
module in order to handle edge cases, improve message decoding quality and is required if
you want to use legacy protocols such as pop3.
Wiki: webklex/php-imap/wiki
Laravel wrapper: webklex/laravel-imap
Table of Contents
- Installation
- Configuration
- Usage
- Basic usage example
- Folder / Mailbox
- oAuth
- Idle
- Search
- Counting messages
- Result limiting
- Pagination
- View examples
- Fetch a specific message
- Message flags
- Attachments
- Advanced fetching
- Events
- Masking
- Specials
- Support
- Documentation
- Client::class
- Message::class
- Folder::class
- Query::class
- Attachment::class
- Mask::class
- MessageMask::class
- AttachmentMask::class
- MessageCollection::class
- AttachmentCollection::class
- FolderCollection::class
- FlagCollection::class
- Known issues
- Milestones & upcoming features
- Security
- Credits
- License
Installation
1.) Install decoding modules:
1.1.) (optional) Install php-imap module if you are having encoding problems:
You might also want to check phpinfo()
if the extensions are enabled.
2.) Now install the PHP-IMAP package by running the following command:
3.) Create your own custom config file like config/imap.php:
Configuration
Supported protocols:
imap
— Use IMAP [default]legacy-imap
— Use the php imap module insteadpop3
— Use POP3nntp
— Use NNTP
The following encryption methods are supported:
false
— Disable encryptionssl
— Use SSLtls
— Use TLSstarttls
— Use STARTTLS (alias TLS) (legacy only)notls
— Use NoTLS (legacy only)
Detailed config/imap.php configuration:
default
— used default account. It will be used as default for any missing account parameters. If however the default account is missing a parameter the package default will be used. Set tofalse
to disable this functionality.accounts
— all available accountsdefault
— The account identifier (in this casedefault
but could also befooBar
etc).host
— imap hostport
— imap portencryption
— desired encryption methodvalidate_cert
— decide weather you want to verify the certificate or notusername
— imap account usernamepassword
— imap account passwordauthentication
— imap authentication method. Useoauth
to use oAuth for Google, etc.
date_format
— The default date format is used to convert any given Carbon::class object into a valid date string. (d-M-Y
,d-M-y
,d M y
)options
— additional fetch optionsdelimiter
— you can use any supported char such as ".", "/", etcfetch
—IMAP::FT_UID
(message marked as read by fetching the message body) orIMAP::FT_PEEK
(fetch the message without setting the "seen" flag)fetch_body
— If set tofalse
all messages will be fetched without the body and any potential attachmentsfetch_flags
— If set tofalse
all messages will be fetched without any flagsmessage_key
— Message key identifier optionfetch_order
— Message fetch ordercommon_folders
— Default folder locations and paths assumed if none is providedopen
— special configuration for imap_open()DISABLE_AUTHENTICATOR
— disable authentication properties.
decoder
— Currently only the message and attachment decoder can be setevents
— Default event handling configmasks
— Default masking configmessage
— Default message maskattachment
— Default attachment mask
Usage
Basic usage example
This is a basic example, which will echo out all Mails within all imap folders and will move every message into INBOX.read. Please be aware that this should not be tested in real life and is only meant to gives an impression on how things work.
Folder / Mailbox
List all available folders:
Get a specific folder:
oAuth
Please take a look at the wiki article for gmail / google mail setup.
Basic oAuth example:
Idle
Every time a new message is received, the server will notify the client and return the new message.
Search for messages
Search for specific emails:
Available search aliases for a better code reading:
All available query / search methods can be found here: Query::class
Available search criteria:
ALL
— return all messages matching the rest of the criteriaANSWERED
— match messages with the \ANSWERED flag setBCC
"string" — match messages with "string" in the Bcc: fieldBEFORE
"date" — match messages with Date: before "date"BODY
"string" — match messages with "string" in the body of the messageCC
"string" — match messages with "string" in the Cc: fieldDELETED
— match deleted messagesFLAGGED
— match messages with the \FLAGGED (sometimes referred to as Important or Urgent) flag setFROM
"string" — match messages with "string" in the From: fieldKEYWORD
"string" — match messages with "string" as a keywordNEW
— match new messagesNOT
— not matchingOLD
— match old messagesON
"date" — match messages with Date: matching "date"RECENT
— match messages with the \RECENT flag setSEEN
— match messages that have been read (the \SEEN flag is set)SINCE
"date" — match messages with Date: after "date"SUBJECT
"string" — match messages with "string" in the Subject:TEXT
"string" — match messages with text "string"TO
"string" — match messages with "string" in the To:UNANSWERED
— match messages that have not been answeredUNDELETED
— match messages that are not deletedUNFLAGGED
— match messages that are not flaggedUNKEYWORD
"string" — match messages that do not have the keyword "string"UNSEEN
— match messages which have not been read yet
Further information:
- http://php.net/manual/en/function.imap-search.php
- https://tools.ietf.org/html/rfc1176
- https://tools.ietf.org/html/rfc1064
- https://tools.ietf.org/html/rfc822
- https://gist.github.com/martinrusev/6121028
Result limiting
Limiting the request emails:
Counting messages
Count all available messages matching the current search criteria:
Pagination
Paginate a query:
Paginate a message collection:
View example for a paginated list:
You can also paginate a Folder-, Attachment- or FlagCollection instance.
View examples
You can find a few blade examples under /examples.
Fetch a specific message
Get a specific message by uid (Please note that the uid is not unique and can change):
Message flags
Flag or "unflag" a message:
Mark all messages as "read" while fetching:
Don't mark all messages as "read" while fetching:
Attachments
Save message attachments:
Advanced fetching
Fetch messages without body fetching (decrease load):
Fetch messages without body, flag and attachment fetching (decrease load):
Change messages fetch order:
Events
The following events are available:
Webklex\PHPIMAP\Events\MessageNewEvent($message)
— can get triggered byFolder::idle
Webklex\PHPIMAP\Events\MessageDeletedEvent($message)
— triggered byMessage::delete
Webklex\PHPIMAP\Events\MessageRestoredEvent($message)
— triggered byMessage::restore
Webklex\PHPIMAP\Events\MessageMovedEvent($old_message, $new_message)
— triggered byMessage::move
Webklex\PHPIMAP\Events\MessageCopiedEvent($old_message, $new_message)
— triggered byMessage::copy
Webklex\PHPIMAP\Events\FlagNewEvent($flag)
— triggered byMessage::setFlag
Webklex\PHPIMAP\Events\FlagDeletedEvent($flag)
— triggered byMessage::unsetFlag
Webklex\PHPIMAP\Events\FolderNewEvent($folder)
— can get triggered byClient::createFolder
Webklex\PHPIMAP\Events\FolderDeletedEvent($folder)
— triggered byFolder::delete
Webklex\PHPIMAP\Events\FolderMovedEvent($old_folder, $new_folder)
— triggered byFolder::move
Create and register your own custom event:
..or set it in your config file under events.message.new
.
Masking
PHP-IMAP already comes with two default masks AttachmentMask::class.
The masked instance has to be called manually and is designed to add custom functionality.
You can call the default mask by calling the mask method without any arguments.
There are several methods available to set the default mask:
The last one wont set the mask but generate a masked instance using the provided mask.
You could also set the default masks inside your config/imap.php
file under masks
.
You can also apply a mask on attachments:
If you want to implement your own mask just extend AttachmentMask::class or Mask::class and implement your desired logic:
Additional examples can be found here:
Specials
Find the folder containing a message:
Support
If you encounter any problems or if you find a bug, please don't hesitate to create a new issue. However please be aware that it might take some time to get an answer. Off topic, rude or abusive issues will be deleted without any notice.
If you need immediate or commercial support, feel free to send me a mail at [email protected].
A little notice
If you write source code in your issue, please consider to format it correctly. This makes it so much nicer to read and people are more likely to comment and help :)
``` php
echo 'your php code...';
```
will turn into:
Features & pull requests
Everyone can contribute to this project. Every pull request will be considered but it can also happen to be declined. To prevent unnecessary work, please consider to create a feature issue first, if you're planning to do bigger changes. Of course you can also create a new feature issue if you're just wishing a feature ;)
Documentation
Client::class
Method | Arguments | Return | Description |
---|---|---|---|
setConfig | array $config | self | Set the Client configuration. Take a look at config/imap.php for more inspiration. |
getConnection | resource $connection | resource | Get the current imap resource |
isConnected | bool | Determine if connection was established. | |
checkConnection | Determine if connection was established and connect if not. | ||
connect | Connect to server. | ||
reconnect | Terminate and reconnect to server. | ||
disconnect | Disconnect from server. | ||
getFolder | string $folder_name, $delimiter = null | Folder | Get a Folder instance by name or path |
getFolders | bool $hierarchical, string or null $parent_folder | FolderCollection | Get folders list. If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. |
openFolder | string or Folder $folder, integer $attempts | Open a given folder. | |
createFolder | string $name | boolean | Create a new folder. |
getQuota | array | Retrieve the quota level settings, and usage statics per mailbox | |
getQuotaRoot | string $quota_root | array | Retrieve the quota settings per user |
expunge | bool | Delete all messages marked for deletion | |
setTimeout | string or int $type, int $timeout | boolean | Set the timeout for certain imap operations: 1: Open, 2: Read, 3: Write, 4: Close |
getTimeout | string or int $type | int | Check current mailbox |
setDefaultMessageMask | string $mask | self | Set the default message mask class |
getDefaultMessageMask | string | Get the current default message mask class name | |
setDefaultAttachmentMask | string $mask | self | Set the default attachment mask class |
getDefaultAttachmentMask | string | Get the current default attachment mask class name | |
getFolderPath | string | Get the current folder path |
Message::class
Method | Arguments | Return | Description |
---|---|---|---|
parseBody | Message | Parse the Message body | |
delete | boolean $expunge | boolean | Delete the current Message |
restore | boolean $expunge | boolean | Restore a deleted Message |
copy | string $mailbox | boolean | Copy the current Messages to a mailbox |
move | string $mailbox, boolean $expunge | boolean | Move the current Messages to a mailbox |
setFlag | string or array $flag | boolean | Set one or many flags |
unsetFlag | string or array $flag | boolean | Unset one or many flags |
hasTextBody | Check if the Message has a text body | ||
hasHTMLBody | Check if the Message has a html body | ||
getTextBody | string | Get the Message text body | |
getHTMLBody | string | Get the Message html body | |
getAttachments | AttachmentCollection | Get all message attachments | |
hasAttachments | boolean | Checks if there are any attachments present | |
getClient | Client | Get the current Client instance | |
getUid | string | Get the current UID | |
getFetchOptions | string | Get the current fetch option | |
getMsglist | integer | Get the current message list | |
getHeaderInfo | object | Get the current header_info object | |
getHeader | string | Get the current raw header | |
getMessageId | string | Get the current message ID | |
getMessageNo | integer | Get the current message number | |
getPriority | integer | Get the current message priority | |
getSubject | string | Get the current subject | |
getReferences | mixed | Get any potentially present references | |
getDate | Carbon | Get the current date object | |
getFrom | array | Get the current from information | |
getTo | array | Get the current to information | |
getCc | array | Get the current cc information | |
getBcc | array | Get the current bcc information | |
getReplyTo | array | Get the current reply to information | |
getInReplyTo | string | Get the current In-Reply-To | |
getSender | array | Get the current sender information | |
getBodies | mixed | Get the current bodies | |
getRawBody | mixed | Get the current raw message body | |
getFlags | FlagCollection | Get the current message flags | |
is | boolean | Does this message match another one? | |
getStructure | object | The raw message structure | |
getFolder | Folder | The current folder | |
mask | string $mask = null | Mask | Get a masked instance |
setMask | string $mask | Message | Set the mask class |
getMask | string | Get the current mask class name |
Folder::class
Method | Arguments | Return | Description |
---|---|---|---|
hasChildren | bool | Determine if folder has children. | |
setChildren | array $children | self | Set children. |
delete | Delete the current Mailbox | ||
subscribe | Subscribe to the current Mailbox | ||
unsubscribe | Unsubscribe from the current Mailbox | ||
idle | callable $callback(Message $new_message) | Idle the current folder | |
move | string $mailbox | Move or Rename the current Mailbox | |
rename | string $mailbox | Move or Rename the current Mailbox | |
getStatus | array | Returns status information on the current mailbox | |
examine | array | Returns status information on the current mailbox | |
appendMessage | string $message, string $options, string $internal_date | bool | Append a string message to the current mailbox |
getClient | Client | Get the current Client instance | |
query | string $charset = 'UTF-8' | WhereQuery | Get the current Client instance |
messages | string $charset = 'UTF-8' | WhereQuery | Alias for Folder::query() |
search | string $charset = 'UTF-8' | WhereQuery | Alias for Folder::query() |
Query::class
Method | Arguments | Return | Description |
---|---|---|---|
where | mixed $criteria, $value = null | WhereQuery | Add new criteria to the current query |
orWhere | Closure $closure | WhereQuery | If supported you can perform extended search requests |
andWhere | Closure $closure | WhereQuery | If supported you can perform extended search requests |
all | WhereQuery | Select all available messages | |
answered | WhereQuery | Select answered messages | |
deleted | WhereQuery | Select deleted messages | |
new | WhereQuery | Select new messages | |
not | WhereQuery | Not select messages | |
old | WhereQuery | Select old messages | |
recent | WhereQuery | Select recent messages | |
seen | WhereQuery | Select seen messages | |
unanswered | WhereQuery | Select unanswered messages | |
undeleted | WhereQuery | Select undeleted messages | |
unflagged | WhereQuery | Select unflagged messages | |
unseen | WhereQuery | Select unseen messages | |
noXSpam | WhereQuery | Select as no xspam flagged messages | |
isXSpam | WhereQuery | Select as xspam flagged messages | |
language | string $value | WhereQuery | Select messages matching a given language |
unkeyword | string $value | WhereQuery | Select messages matching a given unkeyword |
messageId | string $value | WhereQuery | Select messages matching a given message id |
to | string $value | WhereQuery | Select messages matching a given receiver (To:) |
text | string $value | WhereQuery | Select messages matching a given text body |
subject | string $value | WhereQuery | Select messages matching a given subject |
since | string $value | WhereQuery | Select messages since a given date |
on | string $value | WhereQuery | Select messages on a given date |
keyword | string $value | WhereQuery | Select messages matching a given keyword |
from | string $value | WhereQuery | Select messages matching a given sender (From:) |
flagged | string $value | WhereQuery | Select messages matching a given flag |
cc | string $value | WhereQuery | Select messages matching a given receiver (CC:) |
body | string $value | WhereQuery | Select messages matching a given HTML body |
before | string $value | WhereQuery | Select messages before a given date |
bcc | string $value | WhereQuery | Select messages matching a given receiver (BCC:) |
count | integer | Count all available messages matching the current search criteria | |
get | MessageCollection | Fetch messages with the current query | |
limit | integer $limit, integer $page = 1 | WhereQuery | Limit the amount of messages being fetched |
setFetchOptions | boolean $fetch_options | WhereQuery | Set the fetch options |
setFetchBody | boolean $fetch_body | WhereQuery | Set the fetch body option |
setFetchFlags | boolean $fetch_flags | WhereQuery | Set the fetch flags option |
setFetchOrder | string $fetch_order | WhereQuery | Change the fetch ordering (asc is "oldest first", desc is "newest first") |
setFetchOrderAsc | WhereQuery | Change the fetch ordering to ascending | |
setFetchOrderDesc | WhereQuery | Change the fetch ordering to descending | |
leaveUnread | WhereQuery | Don't mark all messages as "read" while fetching: | |
markAsRead | WhereQuery | Mark all messages as "read" while fetching | |
paginate | int $perPage = 5, $page = null, $pageName = 'imap_page' | LengthAwarePaginator | Paginate the current query. |
Attachment::class
Method | Arguments | Return | Description |
---|---|---|---|
getContent | string or null | Get attachment content | |
getMimeType | string or null | Get attachment mime type | |
getExtension | string or null | Get a guessed attachment extension | |
getId | string or null | Get attachment id | |
getName | string or null | Get attachment name | |
getContent | string or null | Get attachment content | |
setSize | int or null | Get attachment size | |
getType | string or null | Get attachment type | |
getDisposition | string or null | Get attachment disposition | |
getContentType | string or null | Get attachment content type | |
save | string $path, string $filename | boolean | Save the attachment content to your filesystem |
mask | string $mask = null | Mask | Get a masked instance |
setMask | string $mask | Attachment | Set the mask class |
getMask | string | Get the current mask class name |
Mask::class
Method | Arguments | Return | Description |
---|---|---|---|
getParent | Masked parent | Get the masked parent object | |
getAttributes | array | Get all cloned attributes | |
__get | mixed | Access any cloned parent attribute | |
__set | mixed | Set any cloned parent attribute | |
__inherit | mixed | All public methods of the given parent are callable |
MessageMask::class
Method | Arguments | Return | Description |
---|---|---|---|
getHtmlBody | string or null | Get HTML body | |
getCustomHTMLBody | callable or bool $callback | string or null | Get a custom HTML body |
getHTMLBodyWithEmbeddedBase64Images | string or null | Get HTML body with embedded base64 images | |
getHTMLBodyWithEmbeddedUrlImages | string $route_name, array $params = [] | string or null | Get HTML body with embedded routed images |
AttachmentMask::class
Method | Arguments | Return | Description |
---|---|---|---|
getContentBase64Encoded | string or null | Get attachment content | |
getImageSrc | string or null | Get attachment mime type |
MessageCollection::class
Extends Illuminate\Support\Collection::class
Method | Arguments | Return | Description |
---|---|---|---|
paginate | int $perPage = 15, $page = null, $pageName = 'page' | LengthAwarePaginator | Paginate the current collection. |
FlagCollection::class
Extends Illuminate\Support\Collection::class
Method | Arguments | Return | Description |
---|---|---|---|
paginate | int $perPage = 15, $page = null, $pageName = 'page' | LengthAwarePaginator | Paginate the current collection. |
AttachmentCollection::class
Extends Illuminate\Support\Collection::class
Method | Arguments | Return | Description |
---|---|---|---|
paginate | int $perPage = 15, $page = null, $pageName = 'page' | LengthAwarePaginator | Paginate the current collection. |
FolderCollection::class
Extends Illuminate\Support\Collection::class
Method | Arguments | Return | Description |
---|---|---|---|
paginate | int $perPage = 15, $page = null, $pageName = 'page' | LengthAwarePaginator | Paginate the current collection. |
Known issues
Error | Solution |
---|---|
Kerberos error: No credentials cache file found (try running kinit) (...) | Uncomment "DISABLE_AUTHENTICATOR" inside and use the legacy-imap protocol |
Change log
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of php-imap with dependencies
ext-openssl Version *
ext-json Version *
ext-mbstring Version *
ext-iconv Version *
ext-fileinfo Version *
nesbot/carbon Version >=1.0
symfony/http-foundation Version >=2.8.0
illuminate/pagination Version >=5.0.0