PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Marcelo Rocha   Email Reader   ???   Download  
File: ???
Role: Documentation
Content type: text/markdown
Description: Auxiliary data
Class: Email Reader
Retrieve email messages from POP3 or IMAP mailbox
Author: By
Last change: Update of README.md
Date: 10 days ago
Size: 3,513 bytes
 

Contents

Class file image Download

EmailMD

A PHP library to read e-mails.

Install with composer

{
    "require": {
        "rochasmarcelo/emailmd": "dev-master"
    }
}

Requirements

  • PHP 5.3.*
  • Imap extension

Example

Getting a MailBox instance

<?php
    require_once 'vendor/autoload.php';
    //Gmail
    $MailBox = EmailMD\MailBoxFactory::gmail(
        '[email protected]',
        'yourpassword'
    );

    //Hotmail / live / msn
    $MailBox = EmailMD\MailBoxFactory::live(
        '[email protected]',
        'yourpassword'
    );

    //Yahoo
    $MailBox = EmailMD\MailBoxFactory::yahoo(
        '[email protected]',
        'yourpassword'
    );

    //Other pop3
    $MailBox = EmailMD\MailBoxFactory::make(
        '{localhost:110/pop3}INBOX',
        '[email protected]',
        'yourpassword'
    );

    //Other imap
    $MailBox = EmailMD\MailBoxFactory::make(
        '{localhost:993/imap/ssl}INBOX',
        '[email protected]',
        'yourpassword'
    );
?>

Basic usage

<?php
    require_once 'vendor/autoload.php';
    //Gmail
    $MailBox = EmailMD\MailBoxFactory::gmail(
        '[email protected]',
        'yourpassword'
    );

    $MailBox->reverse();//Newest message first
    $MailBox->filterSince(new DateTime());//Just message recieved today
    //Get messages
    foreach ( $MailBox as $messageNumber => $message ) {
        echo 'Message number: ' . $messageNumber . PHP_EOL;
        echo $message->getSubject() . PHP_EOL;
    }
?>

Getting just some messages

<?php
    require_once 'vendor/autoload.php';
    //instance
    $MailBox = EmailMD\MailBoxFactory::gmail(
        '[email protected]',
        'yourpassword'
    );

    //Get some messages
    $limit = 10;
    foreach ( $MailBox as $messageNumber => $message ) {
        echo 'Message number: ' . $messageNumber . PHP_EOL;
        echo $message->getSubject() . PHP_EOL;
        $limit--;
        if ( $limit < 1 ) {
            break;
        }
    }
?>

Getting messages recieved since a specific date

<?php
    require_once 'vendor/autoload.php';
    //instance
    $MailBox = EmailMD\MailBoxFactory::gmail(
        '[email protected]',
        'yourpassword'
    );

    //Since today
    $MailBox->filterSince(new DateTime());
    foreach ( $MailBox as $messageNumber => $message ) {
        echo 'Message number: ' . $messageNumber . PHP_EOL;
        echo $message->getSubject() . PHP_EOL;
    }
    //Since yesterday
    $MailBox->filterSince(new DateTime('-1 days'));
    foreach ( $MailBox as $messageNumber => $message ) {
        echo 'Message number: ' . $messageNumber . PHP_EOL;
        echo $message->getSubject() . PHP_EOL;
    }
?>

Getting messages in reverse order

By default the mailbox will return the oldest messages first. But sometimes we need to get the newest messages first, to do so we need to call the "MailBox::reverse" method one time.

<?php
    require_once 'vendor/autoload.php';
    //instance
    $MailBox = EmailMD\MailBoxFactory::gmail(
        '[email protected]',
        'yourpassword'
    );
    $MailBox->reverse();//Now we get the newest first

    //Since today
    $MailBox->filterSince(new DateTime());
    foreach ( $MailBox as $messageNumber => $message ) {
        echo 'Message number: ' . $messageNumber . PHP_EOL;
        echo $message->getSubject() . PHP_EOL;
    }
    $MailBox->reverse();//Now we get the oldest first
?>