Access your Outlook application using PHP

来源:百度文库 编辑:神马文学网 时间:2024/04/29 02:30:49
fromhttp://www.phptalk.com/index.php?page=details&id=3
Access your Outlook application using PHP
Access your Outlook application using PHP
I 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
Registry:
Start regedit.exe from Start Menu->Run ,check the HKEY_CLASSES_ROOT section for the folowing entires
Outlook.Application and MAPI.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 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:
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"

$folder

";
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 "";
}
echo"
N:
Subject

Type
Date
$i
$item->Subject
$item->Type$year/$month/$day
";
}
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 "Message Viewer
No Messages Selected
";
}
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"Message Viewer";
echo"
";
}
}
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"Available folders in this version are:
Inbox($unread)
and Outbox($out_unr)
";
}
//end of classs
}
?>
after this you are ready for the STEP3: Implementation:
Make a new file named: comunread.php
and write:
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:
previous class
require("COutLook.php");
$class= new COutLook;
if no messages selected
if ($id=="" || $folder== ""){
echo "Message Viewer


No Messages Selected
";
}
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 the ‘Warning: Invalid ProgID, GUID string, or Moniker’ error please check your registry for the COM objects Outlook.application and MAPI.Session, or check your syntax.
...
bycontact_bogomil
$i
$items->Subject
$items->Type
$items->Text