medeirosrafael/laravel-imap

Laravel IMAP client (forked)

1.0.2.10 2017-06-07 16:37 UTC

This package is auto-updated.

Last update: 2024-10-19 23:54:13 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

Install

Via Composer

$ composer require webklex/laravel-imap

Setup

Add the service provider to the providers array in config/app.php.

'providers' => [
    Webklex\IMAP\Providers\LaravelServiceProvider::class,
];

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

The following encryption methods are supported:

false   - Disable encryption 
ssl     - Use SSL
tls     - Use TLS

Publishing

You can publish everything at once

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

Access the IMAP Client by its Facade \Webklex\IMAP\Facades\Client. Therefor you might want to add an alias to the aliases array within the config/app.php file.

'aliases' => [
    'Client' => Webklex\IMAP\Facades\Client::class
];

Usage

This library is designed to handle the native php imap functions more easily and to be able to integrate this package within your current laravel installation.

Here 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 ben tested in real live but it gives an impression on how things work.

use Webklex\IMAP\Client;

$oClient = new Client([
    'host'          => 'somehost.com',
    'port'          => 993,
    'encryption'    => 'ssl',
    'validate_cert' => true,
    'username'      => 'username',
    'password'      => 'password',
]);

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

//Get all Mailboxes
$aMailboxes = $oClient->getFolders();

//Loop through every Mailbox
/** @var \Webklex\IMAP\Folder $oMailbox */
foreach($aMailboxes as $oMailbox){

    //Get all Messages of the current Mailbox
    /** @var \Webklex\IMAP\Message $oMessage */
    foreach($oMailbox->getMessages() as $oMessage){
        echo $oMessage->subject.'<br />';
        echo 'Attachments: '.$oMessage->getAttachments()->count().'<br />';
        echo $oMessage->getHTMLBody(true);
        
        //Move the current Message to 'INBOX.read'
        if($oMessage->moveToFolder('INBOX.read') == true){
            echo 'Message has ben moved';
        }else{
            echo 'Message could not be moved';
        }
    }
}

If you use the Facade \Webklex\IMAP\Facades\Client please select an account first:

use Webklex\IMAP\Facades\Client;

$oClient = Webklex\IMAP\Facades\Client::account('default');
$oClient->connect();

You can define your accounts inside the config/imap.php file:

'accounts' => [ 
    'default' => [
        'host'  => env('IMAP_HOST', 'localhost'),
        'port'  => env('IMAP_PORT', 993),
        'encryption'    => env('IMAP_ENCRYPTION', 'ssl'),
        'validate_cert' => env('IMAP_VALIDATE_CERT', true),
        'username' => env('IMAP_USERNAME', 'root@example.com'),
        'password' => env('IMAP_PASSWORD', ''),
    ], 
    'gmail' => [.. ]
]

Documentation

\Webklex\IMAP\Client

\Webklex\IMAP\Message

\Webklex\IMAP\Folder

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email github@webklex.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.