> Articles > PHP articles > Access your Outlook application using PHP
Access your Outlook application using PHPI have always been surprised
from what PHP can do.Using COM objects shows new and best ways for PHP development.
The folowing code, which is designed like Step by Step wizard, shows
the access to Inbox and Outbox folders in your MS Outlook application.
You should do the folowing:
Step1:
check the system configuration and registry entires.
System:
OS: Windows 98/NT/2000/XP
MS Outlook PHP >4.0.5
Registry:
Start regedit.exe from Start
Menu->Run ,check the HKEY_CLASSES_ROOT section for the folowing entires
Outlook.Application and MIME.Session (or MAPI.Session1).If the MAPI.Session
is missing you shold do the folowing:

1.Search your computer for the
file named cdo.dll if doesn't exists (cdo.dll is a part from many products like
xi-tec PhoneOffice) you must download
it from the Microsoft web site .
2.Move your cdo.dll in the system32 directory and register it with the regsvr32.exe(just
drag the file and drop it over regsvr32.exe file).
After this first step you are
ready for the STEP 2 :Classes
Open your favorite PHP editor
open a new file and name it: COutLook.php
It is time to write a simple
class:
<?PHP
global $UnreadMessagesInFolder;
class COutLook{
function for retreiving messages from the selected folder (Inbox or Outbox)
function getMessages($folder){
Setup the folder table,.there is 4 elements:
message number,message subject ,message type and date received
echo"<body
text=darkblue>
<br><font color=red face=verdana size=3><b>$folder</b></font>
<table width=100%>
<TR bgcolor=#EEEFFF><td><font face=verdana size=2>N:</td><td>
<font face=verdana size=2> Subject</td><TD>
<font face=verdana size=2 >Type</TD><TD><font face=verdana
size=2> Date</TD></TR>";
creating the COM instance for Outlook.application and MAPI session(access
the outlook folders object)
$oOutlook = new COM("Outlook.Application");
$session= new COM("MAPI.Session");
Log into the session like default user
$session->Logon();
selecting working folder Inbox ot Outbox/
$inb=$session->$folder;
get the total messages in Folder
$messages=$inb->Messages->Count();
get the elements of the message object
for($i=1;$i<($messages+1);$i++){
$item=$inb->Messages->item($i);
date string
$timeres=$item->TimeReceived();
$date_vb=getdate($timeres);
date elements
$year=$date_vb['year'];
$month=$date_vb['mon'];
$day=$date_vb['mday'];
entering the folder elements
echo "<tr
bgcolor=#F0F0F0><td><font face=verdana size=2 color=darkblue>$i</td><td><font
face=verdana size=2 color=darkblue>
<a href=view.php?id=$i&folder=$folder target=bottomFrame><font
face=verdana size=2 color=#FF6666>$item->Subject</font></td><td><font
face=verdana size=2 color=darkblue>$item->Type</td><td><font
face=verdana size=1 color=darkblue>$year/$month/$day</td></font><tr>";
}
echo"</table>";
}
view mesage from selected folder (Inbox or Outbox)
function ViewMessageFromFolder($id,$folder){
create new instance of the COM Objects
$oOutlook = new COM("Outlook.Application");
$session= new COM("MAPI.Session");
Log into the current working session
$session->Logon();
get default folder
$inb=$session->$folder;
if($id==""){
echo "<font
face=verdana size=2 color=darkblue>Message Viewer</font><br><font
face=verdana size=2 color=red><center>No Messages Selected</center></font>";
}
else{
$idint=(int)$id;
get the messages in the selested folder
$items=$inb->Messages->item($idint);
make message status read= true
$items->Unread="false";
Update the message status into Outlook's Inbox
$items->Update(true);
display the message
echo"<font
face=verdana size=2 color=darkblue>Message Viewer</font>";
echo"<table
width=100%><tr><td><font face=verdana size=2 color=darkblue>$i</td><td><font
face=verdana size=2 color=darkblue>
<b>$items->Subject</b></td><td><font face=verdana
size=2 color=darkblue>$items->Type</td><td></td></font><tr>
<tr><td colspan=4><pre><font face=verdana size=2 color=darkblue>$items->Text</pre></td></tr>";
}
}
function getUnreadinInbox(){
get unread messages from the Inbox Folder
$oOutlook = new COM("Outlook.Application");
$oNs = $oOutlook->GetNamespace("MAPI");
$oFldr = $oNs->GetDefaultFolder(olFolderInbox);
$UnreadMessagesInFolder = $oFldr->UnReadItemCount;
return $UnreadMessagesInFolder;
}
function getUnreadinOutbox(){
get unread messages from the Outbox Folder
$oOutlook = new COM("Outlook.Application");
$oNs = $oOutlook->GetNamespace("MAPI");
$oFldr = $oNs->GetDefaultFolder(olFolderOutbox);
$UnreadMessagesInFolder = $oFldr->UnReadItemCount;
return $UnreadMessagesInFolder;
}
function staticFolders(){
// List of the avaailable folders (static !!!)
$unread=$this->getUnreadinInbox();
$out_unr=$this->getUnreadinOutbox();
echo"<font
color=blue face = verdana size=1>Available folders in this version are:
<a href=comunread.php?folder=Inbox>Inbox(<font color=red>$unread</font>)</a>
and <a href=comunread.php?folder=Outbox>Outbox(<font color=red>$out_unr</font>)</a></font>";
}
//end of classs
}
?>
after this you are ready for the STEP3:
Implementation:
Make a new file named: comunread.php
and write:
<?PHP
previous class
require("COutLook.php");
make new instance of the class
$class= new
COutLook;
if ($folder==""){
$class->staticFolders();
}
else {
$class->staticFolders();
$class->getMessages($folder);
}
?>
and new file named view.php
which contains:
<?PHP
previous class
require("COutLook.php");
$class= new
COutLook;
if no messages selected
if ($id==""
|| $folder== ""){
echo "<font
face=verdana size=2 color=darkblue>Message Viewer</font>
<br><font face=verdana size=2 color=red>
<center>No Messages Selected</center></font>";
}
else{
get the message
$class->ViewMessageFromFolder($id,$folder);
}
?>
and the make a file named index.php which have 2 frames in main frame
you should put the 'comunread.php' file and in the bottom frame 'view.php'
Then run the index.php!
If you have installed Exchange server you must login in your account
when you create the session
$session->Logon("your
name","your password", true or false for the show the logon dialog);
Errors:

If your php returns this error , please check
your registry for the COM objects Outlook.application and MAPI.Session,or
check your syntax.
Written by Bogomil Shopov mailto:admin@purplerain.org
|