Skip to content

Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app.

License

Notifications You must be signed in to change notification settings

oneFoldSoftware/laravel-imap

 
 

Repository files navigation

IMAP Library for Laravel

Latest Version on Packagist Software License Build Status Code quality Total Downloads Hits

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 for a standalone version.

Table of Contents

Documentations

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.

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 >= 5.5, package discovery will configure the service provider and Client alias out of the box. Otherwise, for Laravel <= 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:

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

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
[email protected]
IMAP_PASSWORD=secret
IMAP_DEFAULT_ACCOUNT=default
IMAP_PROTOCOL=imap

Please see webklex/php-imap#Configuration and config/imap.php for a detailed list of all available config options.

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.

use Webklex\PHPIMAP\Client;

$client = new Client([
    '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 = $oClient->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().'<br />';
        echo 'Attachments: '.$message->getAttachments()->count().'<br />';
        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 for more detail and further examples.

Facade

If you use the Facade \Webklex\IMAP\Facades\Client::class, please start by selecting an in 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

You can find a few blade and mask examples under /examples.

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

If you are using google mail or something similar, you might have to use oauth instead:

use Webklex\PHPIMAP\Client;

/** @var \Webklex\PHPIMAP\Client $client */
$client = new Client([
    'host' => 'imap.gmail.com',
    'port' => 993,
    'encryption' => 'ssl',
    'validate_cert' => true,
    'username' => '[email protected]',
    'password' => 'PASSWORD',
    'authentication' => "oauth",
    'protocol' => 'imap'
]);

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

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:

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:

echo 'your php code...';

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 ;)

Known issues

Error Solution
Kerberos error: No credentials cache file found (try running kinit) (...) Uncomment "DISABLE_AUTHENTICATOR" inside and use the legacy-imap protocol config/imap.php

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

Supporters

A special thanks to Jetbrains for supporting this project through their open source license program.

Jetbrains

License

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

About

Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%