Skip to content

Latest commit

 

History

History
237 lines (194 loc) · 7.17 KB

README.md

File metadata and controls

237 lines (194 loc) · 7.17 KB

Laravael-IMAP Example Application

This is an example application to showcase the integration of webklex/laravel-imap.

Table of Contents

Requirements & Preconditions

In this example I'm using a linux based system. This means the commands used below may vary slightly compared to windows.

You need to have the following installed:

  • PHP (php-mbstring & php-mcrypt)
  • Composer

Additional information can be found here: https://www.php-imap.com/installation

Installation & Setup

Start by creating a new Laravel project. I'm going to name my "laravel_imap_example".

composer create-project laravel/laravel laravel_imap_example

Next lets install webklex/laravel-imap

composer require webklex/laravel-imap

In order to use our own custom config, we'll need to publish it first:

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

This will create a new file config/imap.php. This file contains all possible package configurations.

Usage

Account setup & configuration

To use a default account, I'm going to add the following to my .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

Idle command

Let's assume you want to listen for new mails and execute some code if a new mail has arrived. We can realize this by using the IDLE command provided by the imap protocol.

php artisan make:command CustomImapIdleCommand

This will create a new command class inside app/Console/Commands called CustomImapIdleCommand.php. All we need is to modify it and implement the Webklex\IMAP\Commands\ImapIdleCommand class.

<?php  
  
namespace App\Console\Commands;  
  
use Webklex\IMAP\Commands\ImapIdleCommand;  
use Webklex\PHPIMAP\Message;  
  
class CustomImapIdleCommand extends ImapIdleCommand {  
  
    /**  
     * The name and signature of the console command.
     *
	 * @var string  
     */    
     protected $signature = 'custom_command';  
  
    /**  
     * Holds the account information
     *     
     * @var string|array $account  
     */  
    protected $account = "default";  
  
    /**  
     * Callback used for the idle command and triggered for every new received message
     * @param Message $message  
     */  
    public function onNewMessage(Message $message) {  
        $this->info("New message received: " . $message->subject);  
    }  
}

This command should print New message received: the message subject as soon as we received a new mail. So lets test it:

php artisan custom_command

..and sure enough, after sending an email with the subject "test message", it gets printed to the output:

New message received: test message

Additional reference for commands:

Additional methods

There are of course more possibilities than just idling. I'm going to create a new command, called "ImapTest":

php artisan make:command ImapTest
<?php  
  
namespace App\Console\Commands;  
  
use Illuminate\Console\Command;  
use Webklex\IMAP\Facades\Client;  
  
class ImapTest extends Command  
{  
    /**  
     * The name and signature of the console command.
     *
	 * @var string  
     */
     protected $signature = 'imap:test';  
  
    /**  
     * The console command description.
     *
	 * @var string  
     */
     protected $description = 'Command description';  
  
    /**  
     * Execute the console command.
     *
	 * @return int  
     */
     public function handle() {
        $client = Client::account("default");  
        $client->connect();  
  
        /** @var \Webklex\PHPIMAP\Support\FolderCollection $folders */  
        $folders = $client->getFolders(false);  
  
        /** @var \Webklex\PHPIMAP\Folder $folder */  
        foreach($folders as $folder){  
            $this->info("Accessing folder: ".$folder->path);  
  
            $messages = $folder->messages()->all()->limit(3, 0)->get();  
  
            $this->info("Number of messages: ".$messages->count());  
            
            /** @var \Webklex\PHPIMAP\Message $message */  
            foreach ($messages as $message) {  
                $this->info("\tMessage: ".$message->message_id);  
            }  
        }
            
		return 0;  
    }
}

This command will use the default account (as defined inside your .env and config/imap.php) and do the following actions:

  • Fetch all folders
  • Print the path of each found folder
    • Fetch the first 3 messages from that folder
    • Print the message ID of those messages

It might look something like this if executed:

Accessing folder: INBOX
Number of messages: 3
        Message: [email protected]
        Message: [email protected]
        Message: [email protected]
Accessing folder: INBOX.new
Number of messages: 3
        Message: [email protected]
        Message: [email protected]
        Message: [email protected]
Accessing folder: INBOX.9AL56dEMTTgUKOAz
Number of messages: 0
Accessing folder: INBOX.U9PsHCvXxAffYvie
Number of messages: 0
Accessing folder: INBOX.Trash
Number of messages: 3
        Message: [email protected]
        Message: [email protected]
        Message: [email protected]
Accessing folder: INBOX.processing
Number of messages: 1
        Message: [email protected]
Accessing folder: INBOX.Sent
Number of messages: 3
        Message: [email protected]
        Message: [email protected]
        Message: [email protected]
Accessing folder: INBOX.OzDWCXKV3t241koc
Number of messages: 0
Accessing folder: INBOX.5F3bIVTtBcJEqIVe
Number of messages: 0
Accessing folder: INBOX.8J3rll6eOBWnTxIU
Number of messages: 0
Accessing folder: INBOX.Junk
Number of messages: 1
        Message: [email protected]
Accessing folder: INBOX.Drafts
Number of messages: 1
        Message: [email protected]
Accessing folder: INBOX.test
Number of messages: 3
        Message: [email protected]
        Message: [email protected]
        Message: [email protected]

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

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