Giter VIP home page Giter VIP logo

php-imap-client's Introduction

PHP Imap Client

Copyright (c) 2016 Tobias Zeising, [email protected]
http://www.aditu.de
Licensed under the MIT license

Features

This PHP IMAP Client is a simple class for IMAP Email access. It base on the PHP IMAP extension and offers a simple interface for handling emails. In my opinion the PHP IMAP functions are not very intuitive.

  • simple interface
  • get emails and folders
  • move, delete, count emails
  • rename, delete and add folders
  • get attachments

How to use

Instantiating the class will open the imap connection.

$mailbox = 'my.imapserver.com';
$username = 'myuser';
$password = 'secret';
$encryption = 'tls'; // or ssl or '';
$imap = new Imap($mailbox, $username, $password, $encryption);

if($imap->isConnected()===false) {
    die($imap->getError());
}

Now you can fetch all available folders:

$folders = $imap->getFolders(); // returns array of strings
foreach($folders as $folder) {
    echo $folder;
}

Now you can select a folder:

$imap->selectFolder("Inbox");

Once you selected a folder you can count the messages in this folder:

$overallMessages = $imap->countMessages();
$unreadMessages = $imap->countUnreadMessages();

Okay, now lets fetch all emails in the currently selected folder (in our example the "Inbox"):

$emails = $imap->getMessages();
var_dump($emails);

getMessages() will not mark emails as read! It will return the following structure without changing the emails. In this example two emails are in the Inbox.

array(2) {
  [0]=>
  array(8) {
    ["to"]=>
    array(1) {
      [0]=>
      string(30) "Tobias Zeising <[email protected]>"
    }
    ["from"]=>
    string(30) "Karl Mustermann <[email protected]>"
    ["date"]=>
    string(31) "Fri, 27 Dec 2013 18:44:52 +0100"
    ["subject"]=>
    string(12) "Test Subject"
    ["id"]=>
    int(15)
    ["unread"]=>
    bool(true)
    ["answered"]=>
    bool(false)
    ["body"]=>
    string(240) "<p>This is a test body.</p>

    <p>With a bit <em><u>html</u></em>.</p>

    <p>and without <span style="color:#008000"><span style="font-size:14px"><span style="font-family:arial,helvetica,sans-serif">attachment</span></span></span></p>
    "
  }
  [1]=>
  array(9) {
    ["to"]=>
    array(1) {
      [0]=>
      string(29) "[email protected] <[email protected]>"
    }
    ["from"]=>
    string(40) "Karl Ruediger <[email protected]>"
    ["date"]=>
    string(31) "Thu, 19 Dec 2013 17:45:37 +0100"
    ["subject"]=>
    string(19) "Test mit Attachment"
    ["id"]=>
    int(14)
    ["unread"]=>
    bool(false)
    ["answered"]=>
    bool(false)
    ["body"]=>
    string(18) "Anbei eine Datei"
    ["attachments"]=>
    array(1) {
      [0]=>
      array(2) {
        ["name"]=>
        string(24) "640 x 960 (iPhone 4).jpg"
        ["size"]=>
        int(571284)
      }
    }
  }
}

You can also add/rename/delete folders. Lets add a new folder:

$imap->addFolder('archive');

Now we move the first email into this folder

$imap->moveMessage($emails[0]['id'], 'archive');

And we delete the second email from inbox

$imap->deleteMessage($emails[1]['id']);

Methods

Following methods are available.

  • __construct($mailbox, $username, $password, $encryption) open new imap connection
  • isConnected() check whether the imap connection could be opened successfully
  • getError() returns the last error message
  • selectFolder($folder) select current folder
  • getFolders() get all available folders
  • countMessages() count messages in current folder
  • countUnreadMessages() count unread messages in current folder
  • getMessages($withbody = true) get emails in current folder
  • getMessage($id, $withbody = true) get email by given id
  • deleteMessage($id) delete message with given id
  • deleteMessages($ids) delete messages with given ids (as array)
  • moveMessage($id, $target) move message with given id in new folder
  • moveMessages($ids, $target) move messages with given ids (as array) in new folder
  • setUnseenMessage($id, $seen = true) set unseen state of the message with given id
  • getAttachment($id, $index = 0) get attachment of the message with given id (getMessages returns all available attachments)
  • addFolder($name) add new folder with given name
  • removeFolder($name) delete folder with fiven name
  • renameFolder($name, $newname) rename folder with given name
  • purge() move all emails in the current folder into trash. emails in trash and spam will be deleted
  • getAllEmailAddresses() returns all email addresses of all emails (for auto suggestion list)
  • saveMessageInSent($header, $body) save a sent message in sent folder
  • getMailboxStatistics() returns statistics, see imap_mailboxmsginfo

Feedback

Feel free to improve this class. You can use the pull request function of github for contributing improvments. The inner structure of this class is simple and easy. Don't hesitate and check it out ;)

php-imap-client's People

Contributors

andryyy avatar dutchakdev avatar fulldecent avatar henriquesalvan avatar melcak avatar mikewillis avatar natanfelles avatar parturi avatar radub avatar russ-ab avatar ssilence avatar teson2000 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.