aymanrb/mail_filters

There is no license information available for the latest version (v0.0.1-beta) of this package.

A library to help you create Gmail like mail filters in your PHP Application

v0.0.1-beta 2018-09-17 13:50 UTC

README

This is a PHP package that enables you to create handy filtering features on E-mail messages utilizing the PHP IMAP extension. The library is based on ddeboer/imap

With this library you can implement something similar to that of Gmail filters option in your PHP Application.

Features:

  • Apply filters your E-mail messages by setting criteria over the following fields:

    • To
    • All Recipients (To, Cc, Bcc)
    • Subject
    • From
    • Body Text
    • Attachments
  • Take one of the following actions based on matched filters:

    • Delete message from your mailbox.
    • Move the message to a different mail folder.
    • Copy the message to another mail folder.
    • Return user specified variables.
    • Mark message status (Read, Unread and Important)

Example:

//prepare the criteria
$subjectContainsRidCriterion = (new SubjectCriterion())->setValues('*RID*');

//prepare the actions
$returnValuesAction = new ReturnValuesAction(['specialIdSubject' => true]);
$moveToFolderAction = new MoveMailAction(
   $mailbox,
   [ActionsConstantsInterface::PARAM_DESTINATION_DIRECTORY => "New Mail Folder"]
);

//build the filter
$mailFilter = new Filter();
$mailFilter
   ->setName('Mails that contains an RID in the subject')
   ->addCriterion($subjectContainsRidCriterion)
   ->addAction($returnValuesAction)
   ->addAction($moveToFolderAction);

$mailFilters = new MailFilter();
$mailFilters->addFilter($mailFilter);

$server = new Server($config['imap_connection']['imap_server']);
$connection = $server->authenticate($config['imap_connection']['username'], $config['imap_connection']['password']);
$mailbox = $connection->getMailbox('INBOX');

$messages = $mailbox->getMessages();

$mailFilters->applyFilters($messages->current());

if ($mailFilters->isFilterMatched()) {
    print_r($mailFilters->getFilterReturns());
}

The above example will check if the given email contains "RID" in its subject and move the message to a new MailBox "New Mail Folder" and print: Array([specialIdSubject] => 1)

In Progress (Unfinished work):
  • Adding a 'Bounce' or 'Forward' mail "Action"
Future Plans:
  • Adding tests to the library