Email reader and dispatcher powered by Gearman

0.1.2 2019-01-21 21:58 UTC

This package is not auto-updated.

Last update: 2024-07-06 16:45:25 UTC



Build Status Latest Stable Version Total Downloads License

This library will read emails of a specific mailbox, will parse them and finally, dispatch them to handlers. How to install

This library is used to read email (you can inject your own by implementing the DriverInterface) :

Inside our Fetch driver, we used this library to parse the last email reply :

##How is it working ?

  1. This library contains a command which is gonna check all new emails in your mailbox and give them to gearman. 1 new email = 1 new task on gearman.
  2. The gearman job will read the email, parse it and then, dispatch it to your application.
  3. Once gearman is finished with this email, the email is move to another folder to mark it as processed.


Config Default Description
domain Address of your mail serveur (eg.
port 143 SSL port is usually 993, normal is 143
service Imap Imap or Pop
username You email username, most of the time it's your email
password Your email password
main mailbox The mailbox where this library will read new emails
processed mailbox Where to move the emails once processed

###example :

use Smart\EmailReader\Config\EmailServerConfig;

$configs = (EmailServerConfig())


This library come with a dispatcher base class which allows you to handle different kind of email. The handlers on the dispatcher work like a router on the email subject.


Imagine we have support ticket system on our app. We want our customer to be able to reply directly to the email to add their reply in our ticket system.

use Smart\EmailReader\Dispatcher\Dispatcher;

class DispatcherApp extends Dispatcher
    public function __construct()
        $this->addDispatcher(new SupportEmailDispatcher());
        //you can add as many dispatchers as you want....
use Smart\EmailReader\Dispatcher\Dispatcher;
use Smart\EmailReader\EmailEntity;

class SupportEmailDispatcher extends Dispatcher
    public function __construct()
        $this->addHandler('#\[support\-(?<id>[0-9]+)\]#i', [$this, 'handleNewReply']);
    public function handleNewReply($matches, EmailEntity $email)
        $ticketId = isset($matches['id']) ? (int)$matches['id'] : null;
        $newReply = $email->getLastReply();
        //your logic here...

###About dispatchers :

  • The dispatcher don't stop at the first match, different regexs can overlap themself
  • If you return false on a dispatcher, the dispatcher will stop there and complete the task
  • Currently, you can only dispatch the email based on the subject



This should be place in your app container

$emailReader = new Fetch(
    new EmailServerConfig() //check the configuration section

$emailLoggerLogger = new EmailReaderLogger(

###Gearman job

We use Sinergi gearman :

//add this in sinergi gearman :

new EmailReaderDispatchJob(
    new DispatcherApp(), //your own dispatcher

###Register Command

We use symfony console :

$consoleApp = new ConsoleApplication();
$gearmanDispatcher = '...'; //get your gearman dispatcher

    new EmailReaderSendCommand(

Finally, you just need to add a cron on that command every few minutes to read incoming email and dispatch them into your application.