PHPackages                             smartilabs/laravel-imap - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. smartilabs/laravel-imap

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

smartilabs/laravel-imap
=======================

Laravel IMAP client

2.2.0(5y ago)013MITPHPPHP &gt;=5.5.9

Since Jan 19Pushed 5y agoCompare

[ Source](https://github.com/smartilabs/laravel-imap)[ Packagist](https://packagist.org/packages/smartilabs/laravel-imap)[ Docs](https://github.com/webklex/laravel-imap)[ RSS](/packages/smartilabs-laravel-imap/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (74)Used By (0)

IMAP Library for Laravel
========================

[](#imap-library-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/59ee63f9e54f849ec22b7dcead7aff3dafaf40eddf41bc018fddd5df7cd679a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7765626b6c65782f6c61726176656c2d696d61702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Webklex/laravel-imap)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/Webklex/laravel-imap/blob/master/LICENSE)[![Build Status](https://camo.githubusercontent.com/2beb84d3b381e06d4b92add91761a757fa8fd8b8c1b08708f2581a20e9aae343/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f6275696c642f672f5765626b6c65782f6c61726176656c2d696d61702f6d61737465723f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/Webklex/laravel-imap/?branch=master)[![Code quality](https://camo.githubusercontent.com/d09a176fd19a77c4155487bc5f7a27ce003e59ea24f83c201aae6f12d094feca/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f5765626b6c65782f6c61726176656c2d696d61702f6d61737465723f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/Webklex/laravel-imap/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/8dd8f99d06e38b388cf8a6dbd0f1a993a8e999fa9184bf49b832d2c45c987879/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f5765626b6c65782f6c61726176656c2d696d61702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Webklex/laravel-imap)[![Hits](https://camo.githubusercontent.com/4b2e02e4151a526b477ea932f242d8dc2c7f05ec793c65634ac27cc68d4a0399/68747470733a2f2f686974732e7765626b6c65782e636f6d2f7376672f7765626b6c65782f6c61726176656c2d696d61703f)](https://hits.webklex.com)

Description
-----------

[](#description)

Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your **Laravel** app. This enables your app to not only respond to new emails but also allows it to read and parse existing mails and much more.

> If you want to use this library outside of Laravel, please head over to [webklex/php-imap](https://github.com/Webklex/php-imap)for a standalone version.

Table of Contents
-----------------

[](#table-of-contents)

- [Documentations](#documentations)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
    - [Basic usage example](#basic-usage-example)
    - [Facade](#facade)
    - [View examples](#view-examples)
    - [Idle](#idle)
    - [oAuth](#oauth)
    - [Events](#events)
    - [Commands](#commands)
        - [Event driven](#event-driven)
        - [Custom command](#custom-command)
        - [Service setup](#service-setup)
- [Support](#support)
- [Known issues](#known-issues)
- [Security](#security)
- [Credits](#credits)
- [Supporters](#supporters)
- [License](#license)

Documentations
--------------

[](#documentations)

- Legacy (&lt; v2.0.0): [legacy documentation](https://github.com/Webklex/laravel-imap/tree/1.6.2#table-of-contents)
- Core documentation: [webklex/php-imap](https://github.com/Webklex/php-imap)
- Wiki: [php-imap wiki](https://github.com/Webklex/php-imap/wiki)

Installation
------------

[](#installation)

1.) Install the Laravel IMAP package by running the following command:

```
composer require webklex/laravel-imap
```

1.1.) If you are getting errors or having some other issue, please follow step 1. - 1.1 [here](https://github.com/Webklex/php-imap#installation).

1.2.) If you are having trouble with v2.0.0, please go ahead and create a new issue and perhaps try the latest v1.6.2 version:

```
composer require webklex/laravel-imap:1.6.2
```

2.) If you're using Laravel &gt;= 5.5, package discovery will configure the service provider and `Client` alias out of the box. Otherwise, for Laravel &lt;= 5.4, edit your `config/app.php` file and:

- add the following to the `providers` array:

```
Webklex\IMAP\Providers\LaravelServiceProvider::class,
```

- add the following to the `aliases` array:

```
'Client' => Webklex\IMAP\Facades\Client::class,
```

3.) Run the command below to publish the package config file [config/imap.php](src/config/imap.php):

```
php artisan vendor:publish --provider="Webklex\IMAP\Providers\LaravelServiceProvider"
```

Configuration
-------------

[](#configuration)

If you are planning to use a single account, you might want to add the following to your `.env` file.

```
IMAP_HOST=somehost.com
IMAP_PORT=993
IMAP_ENCRYPTION=ssl
IMAP_VALIDATE_CERT=true
IMAP_USERNAME=root@example.com
IMAP_PASSWORD=secret
IMAP_DEFAULT_ACCOUNT=default
IMAP_PROTOCOL=imap

```

Please see [webklex/php-imap#Configuration](https://github.com/Webklex/php-imap#configuration) and [config/imap.php](src/config/imap.php) for a detailed list of all available config options.

Usage
-----

[](#usage)

#### Basic usage example

[](#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.

```
/** @var \Webklex\PHPIMAP\Client $client */
$client = \Webklex\IMAP\Facades\Client::make([
    'host'          => 'somehost.com',
    'port'          => 993,
    'encryption'    => 'ssl',
    'validate_cert' => true,
    'username'      => 'username',
    'password'      => 'password',
    'protocol'      => 'imap'
]);
/* Alternative by using the Facade
$client = Webklex\IMAP\Facades\Client::account('default');
*/

//Connect to the IMAP Server
$client->connect();

//Get all Mailboxes
/** @var \Webklex\PHPIMAP\Support\FolderCollection $folders */
$folders = $client->getFolders();

//Loop through every Mailbox
/** @var \Webklex\PHPIMAP\Folder $folder */
foreach($folders as $folder){

    //Get all Messages of the current Mailbox $folder
    /** @var \Webklex\PHPIMAP\Support\MessageCollection $messages */
    $messages = $folder->messages()->all()->get();

    /** @var \Webklex\PHPIMAP\Message $message */
    foreach($messages as $message){
        echo $message->getSubject().'';
        echo 'Attachments: '.$message->getAttachments()->count().'';
        echo $message->getHTMLBody();

        //Move the current Message to 'INBOX.read'
        if($message->moveToFolder('INBOX.read') == true){
            echo 'Message has ben moved';
        }else{
            echo 'Message could not be moved';
        }
    }
}
```

Please see [webklex/php-imap#Table of Contents](https://github.com/Webklex/php-imap#table-of-contents) for more detail and further examples.

#### Facade

[](#facade)

If you use the Facade [\\Webklex\\IMAP\\Facades\\Client::class](src/IMAP/Facades/Client.php), please start by selecting an in [config/imap.php](src/config/imap.php) defined account first followed by `Client::connect()` to establish an authenticated connection:

```
use Webklex\IMAP\Facades\Client;

/** @var \Webklex\PHPIMAP\Client $client */
$client = Client::account('default');
$client->connect();
```

#### View examples

[](#view-examples)

You can find a few blade and [mask](https://github.com/Webklex/php-imap#masking) examples under [/examples](examples).

#### Idle

[](#idle)

Every time a new message is received, the server will notify the client and return the new message.

The callback and `Webklex\IMAP\Events\MessageNewEvent($message)` event get fired by every new incoming email.

```
$timeout = 1200;
/** @var \Webklex\PHPIMAP\Folder $folder */
$folder->idle(function($message){
    /** @var \Webklex\PHPIMAP\Message $message */
    dump("new message", $message->subject);
}, $timeout);
```

#### oAuth

[](#oauth)

Please take a look at [the wiki article](https://github.com/Webklex/php-imap/wiki/Google-Mail---Gmail) for gmail / google mail setup.

Basic oAuth example:

```
/** @var \Webklex\PHPIMAP\Client $client */
$client = \Webklex\IMAP\Facades\Client::make([
    'host'          => 'somehost.com',
    'port'          => 993,
    'encryption'    => 'ssl',
    'validate_cert' => true,
    'username'      => 'example@gmail.com',
    'password'      => 'ACCESS-TOKEN',
    'authentication' => "oauth",
    'protocol'      => 'imap'
]);

//Connect to the IMAP Server
$client->connect();
```

#### Events

[](#events)

The following events are available:

- `Webklex\IMAP\Events\MessageNewEvent($message)` — can get triggered by `Folder::idle`
- `Webklex\IMAP\Events\MessageDeletedEvent($message)` — triggered by `Message::delete`
- `Webklex\IMAP\Events\MessageRestoredEvent($message)` — triggered by `Message::restore`
- `Webklex\IMAP\Events\MessageMovedEvent($old_message, $new_message)` — triggered by `Message::move`
- `Webklex\IMAP\Events\MessageCopiedEvent($old_message, $new_message)` — triggered by `Message::copy`
- `Webklex\IMAP\Events\FlagNewEvent($flag)` — triggered by `Message::setFlag`
- `Webklex\IMAP\Events\FlagDeletedEvent($flag)` — triggered by `Message::unsetFlag`
- `Webklex\IMAP\Events\FolderNewEvent($folder)` — can get triggered by `Client::createFolder`
- `Webklex\IMAP\Events\FolderDeletedEvent($folder)` — triggered by `Folder::delete`
- `Webklex\IMAP\Events\FolderMovedEvent($old_folder, $new_folder)` — triggered by `Folder::move`

Additional integration information:

-
-
-

Commands
========

[](#commands)

Let's assume you want to run the imap idle process in the background of your server to automatically handle new messages. The following examples will show two major ways to archive this:

### Event driven

[](#event-driven)

Start by adding the following to your `app/Console/Kernel.php` file:

```
/**
 * The Artisan commands provided by your application.
 *
 * @var array
 */
protected $commands = [
    \Webklex\IMAP\Commands\ImapIdleCommand::class,
];
```

Now register an event listener as described by [the laravel docs](https://laravel.com/docs/7.x/events#event-subscribers). If you don't use the default account, or if you want to add some of your own magic, you'll need to create a custom command (see next section).

Finally test the command by running:

```
php artisan imap:idle
```

### Custom Command

[](#custom-command)

Create a new file like `app/Console/Commands/CustomImapIdleCommand.php` and add the following:

```
