2017-08-08 13:03:22 +02:00
|
|
|
Imap
|
|
|
|
====
|
|
|
|
:mailbox_with_mail: Access Mailbox Using PHP IMAP.
|
|
|
|
|
2019-11-13 12:22:12 +01:00
|
|
|
*Current Version: 1.0.6*
|
2017-08-12 12:56:59 +02:00
|
|
|
|
|
|
|
[![Build Status](https://travis-ci.org/Clivern/Imap.svg?branch=master)](https://travis-ci.org/Clivern/Imap)
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
2017-08-15 15:58:49 +02:00
|
|
|
|
2017-08-14 14:18:20 +02:00
|
|
|
To install the package via `composer`, use the following:
|
2017-08-15 15:58:49 +02:00
|
|
|
|
2017-08-12 12:56:59 +02:00
|
|
|
```php
|
2017-08-14 14:28:37 +02:00
|
|
|
composer require clivern/imap
|
2017-08-12 12:56:59 +02:00
|
|
|
```
|
2017-08-15 15:58:49 +02:00
|
|
|
This command requires you to have Composer installed globally.
|
|
|
|
|
2017-08-12 12:56:59 +02:00
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
2017-08-15 15:58:49 +02:00
|
|
|
|
2017-08-12 12:56:59 +02:00
|
|
|
After adding the package as a dependency, Please read the following steps:
|
|
|
|
|
2017-08-15 15:58:49 +02:00
|
|
|
### Connect and Authenticate
|
|
|
|
|
2017-08-14 14:18:20 +02:00
|
|
|
```php
|
|
|
|
include_once dirname(__FILE__) . '/vendor/autoload.php';
|
|
|
|
|
2017-08-15 16:08:02 +02:00
|
|
|
use Clivern\Imap\Core\Connection;
|
|
|
|
|
|
|
|
$connection = new Connection(
|
2017-08-15 17:40:01 +02:00
|
|
|
"imap.gmail.com",
|
|
|
|
"993",
|
|
|
|
"test@clivern.com",
|
|
|
|
"my_password",
|
|
|
|
"/ssl",
|
|
|
|
"INBOX"
|
2017-08-15 16:00:33 +02:00
|
|
|
);
|
2017-08-14 14:18:20 +02:00
|
|
|
$connection->connect();
|
|
|
|
```
|
|
|
|
|
2017-09-02 15:33:59 +02:00
|
|
|
After end of everything, you should close connection
|
|
|
|
|
|
|
|
```php
|
|
|
|
$connection->disconnect();
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2017-08-15 15:58:49 +02:00
|
|
|
#### Connection Options
|
|
|
|
|
|
|
|
```php
|
2017-08-15 17:26:55 +02:00
|
|
|
$folder = "INBOX";
|
|
|
|
|
|
|
|
// Reconnect & Update Mailbox Folder
|
|
|
|
$connection->reconnect($folder);
|
|
|
|
// Reopen Connection
|
|
|
|
$connection->survive($folder);
|
|
|
|
// Get Connection Stream
|
|
|
|
$connection->getStream();
|
|
|
|
// Get Server String
|
|
|
|
$connection->getServer();
|
|
|
|
// Check Connection
|
|
|
|
$connection->checkConnection();
|
|
|
|
// Get Quota Array
|
|
|
|
$connection->getQuota($folder);
|
|
|
|
// Get Status Array
|
|
|
|
$connection->getStatus($folder);
|
|
|
|
// Check MailBox Data
|
|
|
|
$connection->check();
|
|
|
|
// Ping Connection
|
|
|
|
$connection->ping();
|
|
|
|
// Get Errors
|
|
|
|
$connection->getErrors();
|
|
|
|
// Get Alerts
|
|
|
|
$connection->getAlerts();
|
|
|
|
// Get Last Error
|
|
|
|
$connection->getLastError();
|
|
|
|
// Disconnect
|
|
|
|
$connection->disconnect();
|
2017-08-15 15:58:49 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Mailboxes
|
|
|
|
|
|
|
|
Retrieve mailboxes (also known as mail folders) from the mail server and iterate over them:
|
|
|
|
|
|
|
|
```php
|
2017-08-15 16:08:02 +02:00
|
|
|
use Clivern\Imap\MailBox;
|
|
|
|
|
|
|
|
$mailbox = new MailBox($connection);
|
2017-08-15 15:58:49 +02:00
|
|
|
|
|
|
|
$messages = $mailbox->getMessages();
|
|
|
|
|
|
|
|
foreach ($messages as $message) {
|
2017-08-15 17:40:01 +02:00
|
|
|
echo "Subject: " . $message->header()->get('subject');
|
|
|
|
echo "<br/>";
|
|
|
|
echo $message->body()->getMessage();
|
2017-08-15 15:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Searching
|
|
|
|
|
2017-08-14 14:18:20 +02:00
|
|
|
To add custom search
|
2017-08-15 15:58:49 +02:00
|
|
|
|
2017-08-14 14:18:20 +02:00
|
|
|
```php
|
2017-08-15 16:08:02 +02:00
|
|
|
use Clivern\Imap\Core\Search;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\All;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Answered;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\BCC;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Before;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Body;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\CC;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Deleted;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Flagged;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\From;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Keyword;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\NewFlag;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Old;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\On;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Recent;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Seen;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Since;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Subject;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\Text;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\To;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\UnAnswered;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\UnDeleted;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\UnFlagged;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\UnKeyword;
|
|
|
|
use Clivern\Imap\Core\Search\Condition\UnSeen;
|
|
|
|
|
|
|
|
$search = new Search();
|
|
|
|
$search->addCondition(new All());
|
|
|
|
// $search->addCondition(new Answered());
|
|
|
|
// $search->addCondition(new BCC("filter@gmail.com"));
|
|
|
|
// $search->addCondition(new Before(date("j F Y")));
|
|
|
|
// $search->addCondition(new Body("search text"));
|
|
|
|
// $search->addCondition(new CC("filter@gmail.com"));
|
|
|
|
// $search->addCondition(new Deleted());
|
|
|
|
// $search->addCondition(new Flagged());
|
|
|
|
// $search->addCondition(new From("filter@gmail.com"));
|
|
|
|
// $search->addCondition(new Keyword("test"));
|
|
|
|
// $search->addCondition(new NewFlag());
|
|
|
|
// $search->addCondition(new Old());
|
|
|
|
// $search->addCondition(new On(date("j F Y")));
|
|
|
|
// $search->addCondition(new Recent());
|
|
|
|
// $search->addCondition(new Seen());
|
|
|
|
// $search->addCondition(new Since(date("j F Y")));
|
|
|
|
// $search->addCondition(new Subject("search text"));
|
|
|
|
// $search->addCondition(new Text("search text"));
|
|
|
|
// $search->addCondition(new To("filter@gmail.com"));
|
|
|
|
// $search->addCondition(new UnAnswered());
|
|
|
|
// $search->addCondition(new UnDeleted());
|
|
|
|
// $search->addCondition(new UnFlagged());
|
|
|
|
// $search->addCondition(new UnKeyword("test"));
|
|
|
|
// $search->addCondition(new UnSeen());
|
2017-08-14 14:18:20 +02:00
|
|
|
|
|
|
|
// For more info, please check http://php.net/manual/en/function.imap-search.php
|
|
|
|
```
|
|
|
|
|
|
|
|
Then configure mailbox:
|
|
|
|
|
|
|
|
```php
|
2017-08-15 16:08:02 +02:00
|
|
|
use Clivern\Imap\MailBox;
|
|
|
|
|
|
|
|
$mailbox = new MailBox($connection);
|
2017-08-14 14:18:20 +02:00
|
|
|
|
|
|
|
$messages = $mailbox->getMessages($search);
|
|
|
|
|
|
|
|
foreach ($messages as $message) {
|
2017-08-15 17:40:01 +02:00
|
|
|
echo "Subject: " . $message->header()->get('subject');
|
|
|
|
echo "<br/>";
|
|
|
|
echo $message->body()->getMessage();
|
2017-08-14 14:18:20 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-08-15 17:26:55 +02:00
|
|
|
#### Mailbox Option
|
|
|
|
|
2017-08-15 17:37:39 +02:00
|
|
|
Some good methods in mailbox
|
|
|
|
|
2017-08-15 17:26:55 +02:00
|
|
|
```php
|
|
|
|
use Clivern\Imap\MailBox;
|
|
|
|
|
|
|
|
$mailbox = new MailBox($connection);
|
|
|
|
// Get Folders
|
|
|
|
$mailbox->getFolders();
|
|
|
|
// Update Folder
|
|
|
|
$mailbox->setFolder("[Gmail]/All Mail");
|
|
|
|
// Count Messages in Current Folder
|
|
|
|
$mailbox->count();
|
|
|
|
|
|
|
|
$messages = $mailbox->getMessages();
|
|
|
|
|
|
|
|
foreach ($messages as $message) {
|
2017-08-15 17:40:01 +02:00
|
|
|
echo "Subject: " . $message->header()->get('subject');
|
|
|
|
echo "<br/>";
|
|
|
|
echo $message->body()->getMessage();
|
2017-08-15 17:26:55 +02:00
|
|
|
}
|
|
|
|
```
|
2017-08-12 12:56:59 +02:00
|
|
|
|
2017-08-15 15:58:49 +02:00
|
|
|
#### Messages
|
2017-08-15 17:37:39 +02:00
|
|
|
|
|
|
|
To get message header data:
|
|
|
|
|
|
|
|
```php
|
|
|
|
$message->header()->get('subject');
|
|
|
|
$message->header()->get('from');
|
|
|
|
$message->header()->get('to');
|
|
|
|
$message->header()->get('date');
|
|
|
|
$message->header()->get('message_id');
|
|
|
|
$message->header()->get('in_reply_to');
|
|
|
|
$message->header()->get('references');
|
|
|
|
$message->header()->get('size');
|
|
|
|
$message->header()->get('uid');
|
|
|
|
$message->header()->get('msgno');
|
|
|
|
$message->header()->get('recent');
|
|
|
|
$message->header()->get('flagged');
|
|
|
|
$message->header()->get('answered');
|
|
|
|
$message->header()->get('deleted');
|
|
|
|
$message->header()->get('seen');
|
|
|
|
$message->header()->get('draft');
|
|
|
|
$message->header()->get('udate');
|
|
|
|
```
|
|
|
|
|
|
|
|
To get message body
|
|
|
|
|
|
|
|
```php
|
|
|
|
$message->body()->getMessage();
|
|
|
|
$message->body()->getEncoding();
|
|
|
|
```
|
|
|
|
|
|
|
|
To get message attachments
|
|
|
|
|
2017-08-15 15:58:49 +02:00
|
|
|
```php
|
2017-08-15 17:37:39 +02:00
|
|
|
$attachments = $message->attachments();
|
|
|
|
foreach ($attachments as $attachment) {
|
2017-08-15 17:40:01 +02:00
|
|
|
$attachment->getFilename();
|
|
|
|
$attachment->getExtension();
|
|
|
|
$attachment->getSize();
|
|
|
|
$attachment->getEncoding();
|
|
|
|
$attachment->getBytes();
|
|
|
|
// get attachment content
|
|
|
|
$attachment->getPlainBody();
|
|
|
|
// get decoded attachment content
|
|
|
|
$attachment->getBody();
|
|
|
|
// Store attachment in provided path
|
|
|
|
$attachment->store(__DIR__ . '/');
|
2017-08-15 17:37:39 +02:00
|
|
|
}
|
2017-08-15 15:58:49 +02:00
|
|
|
```
|
|
|
|
|
2017-09-02 15:33:59 +02:00
|
|
|
To do actions on message like delete or undelete
|
|
|
|
|
|
|
|
```php
|
|
|
|
$message->action()->delete();
|
|
|
|
$message->action()->undelete();
|
|
|
|
// and don't forget to run the following to delete all messages marked for deletion
|
|
|
|
$mailbox->expunge();
|
|
|
|
```
|
|
|
|
|
2017-08-15 15:58:49 +02:00
|
|
|
|
2017-08-12 12:56:59 +02:00
|
|
|
Misc
|
|
|
|
====
|
|
|
|
|
|
|
|
Changelog
|
|
|
|
---------
|
2019-11-13 12:22:12 +01:00
|
|
|
Version 1.0.6:
|
|
|
|
```
|
|
|
|
Fix Class Name.
|
|
|
|
```
|
|
|
|
|
2019-02-25 19:29:39 +01:00
|
|
|
Version 1.0.5:
|
|
|
|
```
|
|
|
|
Enhance code style.
|
|
|
|
Automate code fixes and linting.
|
|
|
|
```
|
|
|
|
|
2019-02-25 14:47:07 +01:00
|
|
|
Version 1.0.4:
|
|
|
|
```
|
|
|
|
Fix for plain text messages.
|
|
|
|
```
|
|
|
|
|
2018-11-12 14:28:14 +01:00
|
|
|
Version 1.0.3:
|
|
|
|
```
|
|
|
|
Fix Attachment Object.
|
|
|
|
```
|
|
|
|
|
2017-09-02 15:37:24 +02:00
|
|
|
Version 1.0.2:
|
|
|
|
```
|
2018-11-12 14:28:14 +01:00
|
|
|
Message delete & undelete actions added.
|
2017-09-02 15:37:24 +02:00
|
|
|
```
|
|
|
|
|
2017-08-18 13:36:39 +02:00
|
|
|
Version 1.0.1:
|
|
|
|
```
|
|
|
|
Debug data removed.
|
|
|
|
```
|
|
|
|
|
2017-08-12 12:56:59 +02:00
|
|
|
Version 1.0.0:
|
|
|
|
```
|
2017-08-15 17:37:39 +02:00
|
|
|
Initial Release.
|
2017-08-12 12:56:59 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Acknowledgements
|
|
|
|
----------------
|
|
|
|
|
2019-11-13 12:22:12 +01:00
|
|
|
© 2019, Clivern. Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
2017-08-12 12:56:59 +02:00
|
|
|
|
2019-02-25 14:55:29 +01:00
|
|
|
**Imap** is authored and maintained by [@clivern](http://github.com/clivern).
|