PHP/Outlook integration read folders with com_load_typelib

i'm trying to do a PHP/Outlook integration with com_load_typelib("outlook.application");. At the moment, i'm able to send email but is there a way to read some email in a specific folder and put part of content in an excel file?

Now i can count the number of element in my inbox with this code:

$olFolderInbox = 6;

com_load_typelib("Outlook.Application");
echo "Outlook loaded...";

$Outlook = new COM("Outlook.Application") or die("Unable to load Outlook COM");
echo "com object created...";

$OutlookMAPI_Ns = $Outlook->GetNamespace("MAPI") or die("Unable to get Namespace");
echo "namespace created...";

$def_fld_inbox = $OutlookMAPI_Ns->GetDefaultFolder($olFolderInbox) or die("Unable to get olFolderInbox");
echo "got default folder.<br>";

$items_in_folder = $def_fld_inbox->Items->Count();

echo "Items in Inbox: $items_in_folder";

$Outlook->Quit();

echo "Outlook Closed.";

How can i work on a specific SubFolders and parse some information and put part of content in an excel file?

Answer

Solution:

If your code is run on the server-side, Outlook can't be automated from the server-side. Pay attention to the following information:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

Read more about that in the Considerations for server-side Automation of Office article.

As a possible workaround you may consider using Graph API (for Office 365) and EWS for Exchange on-premise accounts, see Explore the EWS Managed API, EWS, and web services in Exchange.


Use the Folders property which returns the Folders collection that represents all the folders contained in the specified Folder. See Enumerate folders for more information.

Source