3. Getting Started

The PHP-MAPI extension provides access to MS MAPI function from php. Although not all MAPI functions and interfaces are supported, most functions have a php counterpart in this extension. Using PHP-MAPI, users can create webbased e-mail and calendaring systems and interfaces with existing php projects, using the mapi functions like a normal mapi program.

3.1. General MAPI Functions

If you work with mapi there are a few operations which will be generally used, these functions are used to interact with MAPI Objects.

The userstore (mapimsgstore) contains the Calendar, Mail, Notes etc. - These items are stored in a mapifolder and a mapifolder contains a mapitable which has mapimessages.

A MAPI message contains a list of properties, for example the subject and two tables: recipienttable and attachmenttable.

+> userstore
|
+-+> Inbox (mapifolder)
   |
   +--+> mapitable
   |  |
   |  +--+> mapimessage
   |     |
   |     +--+> properties
   |     |
   |     +--+> attachments
   |     |
   |     +--+> recipients
   |
   +> Calendar (mapifolder)
   |
   +--+> mapitable
   |
   +--+> mapimessage

There are a few functions which apply to these different items

3.1.1. mapi_getprops

mapi_getprops ( $obj, $properties)

This function returns an associative array of the requested properties and their values. If the value is not available, the value is not added to the associative array. Returns an array on success, FALSE on failure.

$obj The object which you are requesting the properties of. $properties (optional). The properties that you want to read from the object.

3.2. Handling MAPI Items

3.3. Opening a mail

Opening a message in PHP-MAPI requires a few operations, which are needed to get the default store, open the inbox folder and reading the properties of a MAPI message.

# PHP-MAPI provides these files
include('/usr/share/php/mapi/mapi.util.php');
include('/usr/share/php/mapi/mapidefs.php');
include('/usr/share/php/mapi/mapicode.php');
include('/usr/share/php/mapi/mapitags.php');
include('/usr/share/php/mapi/mapiguid.php');

$session = mapi_logon_zarafa('user','password');

$msgstorestable = mapi_getmsgstorestable($session);
$msgstores = mapi_table_queryallrows($msgstorestable, array(PR_DEFAULT_STORE, PR_ENTRYID));
foreach ($msgstores as $row) {
        if($row[PR_DEFAULT_STORE]) {
                $storeentryid = $row[PR_ENTRYID];
        }
}

$userstore = mapi_openmsgstore($session, $storeentryid);
$inbox = mapi_msgstore_getreceivefolder($userstore);
$table = mapi_folder_getcontentstable($inbox);
$items = mapi_table_queryallrows($table,array(PR_SUBJECT, PR_ENTRYID));

foreach($items as $item) {
        print($item[PR_SUBJECT] . "\n");
}

The code above opens a user session, then looks for the defaultstore and opens it. Now we can use the mapi_msgstore_getreceivefolder() to open the inbox folder. Now that we have obtained the mapifolder we need to obtain mapitable so that we can iterate over it, when using mapi_table_queryallrows() we always needs to define which MAPI properties we want to have for this example I’m only interested in the entryid and the subject.

The variable $items contains an array which can contains multiple associative arrays which have a array(Property - Value) mapping. A few of these properties are explained in the table below.

Example of MAPI Properties of an email message.

Property Explanation
PR_SUBJECT Message subject
PR_PRIORITY Priority of E-Mail
PR_IMPORTANCE Importance of E-Mail
PR_BODY Body of E-Mail

3.4. Reading recipients

Recipients of an email are stored in a separate MAPITable of a MAPI Object. To list the recipients of a message, we call mapi_message_getrecipienttable on a MAPI Object and mapi_table_queryallrows to fetch all the entries in the table.

$message = mapi_msgstore_openentry($store, $item[PR_ENTRYID]);
$table = mapi_message_getrecipienttable($message);
$recips = mapi_table_queryallrows($table, array(PR_DISPLAY_NAME, PR_ADDRTYPE, PR_EMAIL_ADDRESS));
foreach($recips as $recip) {
        print $recip[PR_ADDRTYPE] . "\n";
        print $recip[PR_DISPLAY_NAME] . "\n";
        print $recip[PR_EMAIL_ADDRESS] . "\n";
}

3.5. Reading attachments

Attachments are also stored in a separate table of a MAPI Object. The example is similar to reading recipients, it also saves the attachments to disk.

$message = mapi_msgstore_openentry($store, $entry[PR_ENTRYID]);
$table = mapi_message_getattachmenttable($message);
$attachments = mapi_table_queryallrows($table, array(PR_ATTACH_NUM, PR_ATTACH_SIZE, PR_ATTACH_LONG_FILENAME));
foreach($attachments as $attach) {
        $filename = $attach[PR_ATTACH_LONG_FILENAME];
        print 'Attach Name ' . $filename  .' Attach number: ' . $attach[PR_ATTACH_NUM] . ' | Size ' .  $attach[PR_ATTACH_SIZE] . "\n";

        $attach = mapi_message_openattach ($message, $attach[PR_ATTACH_NUM]);
        $stream = mapi_openpropertytostream($attach, PR_ATTACH_DATA_BIN);
        $stat = mapi_stream_stat($stream);

        $fhandle = fopen("$filename", 'w');
        $buffer = null;

        for($i = 0; $i < $stat["cb"]; $i += 1024) {
                // Write stream
                $buffer = mapi_stream_read($stream, 1024);
                fwrite($fhandle, $buffer, strlen($buffer));
        }
        fclose($fhandle);
}