Creating Outlook Tasks with PHP
Last week I posted a script I'm using to make it easier to create a weekly milestone/status report. In my case, it's a specific client that wants these, but I know lots of employees who are required to do them as well.
Here's another piece in what is shaping up to be a string of tools for managing this kind of thing. This creates an Outlook task for taking daily notes on what happened each day. It will be run each Monday to create a task with a due date of Friday. The subject will contain the dates for the Sun-Sat current week.
It also serves as a fairly simple example of creating tasks in Outlook automatically. The code is commented to explain what's going on. As with the previous solution, I'm using the Windows task scheduler to have this kick off on Monday morning. I'll likely write something up to easily "self-schedule" as well as I am already irritated at having to manually set these up even the once. (I know, low tolerance for irritability).
< ?php
/* Define Outlook number for Task Item */
$olTaskItem = 3;
/* Create an instance of Outlook */
$ol = new COM("Outlook.Application");
/* Create a Task Item */
$task = $ol->CreateItem($olTaskItem);
/*
Set dates for current week.
PHP's strtotime needs the chosen strings to give us the Sun-Sat week.
If you choose to use a Mon-Fri week, the "last" becomes "this" for the beginning.
It's just how PHP and strtotime work.
*/
$begin = date("m/d/y", strtotime("last Sunday"));
$end = date("m/d/y", strtotime("this Saturday"));
$due = date("m/d/y", strtotime("this Friday"));
/* Set some properties of the task */
$task->Subject = "Milestone Notes for $begin to $end";
$task->DueDate = $due;
$task->Body = "Insert the template text for your task";
/* Save the Task */
$task->Save();
print("Done");
?>
If you're curious about modifying the task a little more, you can see the complete list of properties (Subject, Body, DueDate, etc.) and Methods (like Save) in the Outlook documentation for the TaskItem. Most of the examples are in VBScript, but that's generally the case with Office documentation. However, use my sample script as your guide and you can pretty quickly see the syntax conversion you need to do to make their examples work (is there interest in a post showing how to more generally convert VBScript or Javascript into PHP?).

February 18th, 2006 at 10:13 pm
I am in IT for small company and our internal software is all done in PHP. I know PHP very well, but my knowledge of COM stuff is very weak. Our telephone system (Televantage) has a great API for doing all sorts of stuff, but its all COM based. There were a few sample scripts (VB i believe) but was having trouble implementing them in ASP (which I dont know well at all) and I really am struggling with trying to take those scripts and convert them for use in PHP with the COM object. So any help you could give would be great. Also, is it possible to create a task in outlook and assign it to someone else? We want our managers to be able to mark an item for an employee, and have it auto generate an outlook task for that employee.
Thanks for your help
February 19th, 2006 at 8:51 am
I did start on my "porting ASP to PHP" article, and I'll work on finishing it up when I get a chance. If you can send me a link to their documentation, I might be able to use it as an example in my posting using their objects.
The biggest syntax change is that PHP uses $object->property syntax while VBScript uses object.property. Also, whenever you see a constant like
myOlApp.CreateItem(olTaskItem)
the "olTaskItem" is a constant that you'll have to look up. The constant is often just "there" if you're doing it in VBA, but in PHP, you'll need the actual value of "3" instead.
In Outlook, you can defintely assign tasks. Effectively, you can do pretty much anything with COM in Outlook that you can do manually. That's really the power of the automation stuff. In the above code, once you reach the point where you're setting properties:
$task->Subject = "Subject";
The variable $task is what the Outlook object model calls an "Item". It's specifically a TaskItem. If you look in the Outlook documentation at the object "TaskItem", you will see all of the properties and methods you can use on a task.
The Outlook object model is described here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landoffice03_vba.asp
TaskItems are described here:
http://msdn.microsoft.com/library/en-us/vbaol11/html/olobjTaskItem_HV05247372.asp?frame=true
Down at the bottom of that documentation page is a huge list of those properties an methods. "Save()" is one of those that I used in my sample. For your project, you'll want to look at "Assign" to send them out and possibly "Mark Complete" and "Delete" if you want to cancel from the same place you're assigning.
Take a look at the VB examples and compare them with my PHP above and you should see the patterns you'll need to convert all of the example code into PHP.
Oh. You may also need to install a program that I don't have the name/URL of handy that overrides Outlook's tight security to enable your app to really dig into Outlook.
March 9th, 2006 at 10:55 am
How I get the number for OlForderContacts? because I got this error when I tried to use other folder.
Warning: (null)(): Invoke() failed: Type mismatch. Argument: 2
March 9th, 2006 at 11:05 am
All of the outlook constants are defined in the documentation I linked to.
March 20th, 2006 at 11:53 am
Hi!
i have tested this code but i think there is something wrong. i have outlook 2003, win xp, and php 4, is there any issue with outlook 2003 and the foloing code?
sorry about my english (i'm portugese)
thanks
April 17th, 2006 at 2:52 am
Hi,
Need to know where I can find list of valid commands, ie, Worksheet->Add(); for excel COM objects.
Can you help?
April 17th, 2006 at 4:17 am
Just go up the tree from the documentation I linked to. All of the Office objects are there.
May 27th, 2006 at 10:59 am
this code seems to working for me. cheers!
June 30th, 2006 at 11:52 pm
I am trying only the code
But getting the error as :
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `Outlook.Application.11': Server execution failed ' in C:\Program Files\Apache Group\Apache2\htdocs\powerplate\test4.php:2 Stack trace: #0 C:\Program Files\Apache Group\Apache2\htdocs\powerplate\test4.php(2): com->com('Outlook.Applica…') #1 {main} thrown in C:\Program Files\Apache Group\Apache2\htdocs\powerplate\test4.php on line 2
I have isntalled Windows XP / MSOutlook 2003 /Apache 2.054 /PHP 5.0.3
Please help
April 5th, 2007 at 3:54 am
com functions are NOT available under a linux distribution - windows only, you will have to resort to an activeXObject instead!
October 30th, 2007 at 6:44 am
hi there!
as i want to develop a webbased tool in PHP4, which sends tasks and meetings to my outlook as an email (as if somebody else would send me the task from his outlook), i wonder, how the task can be sent.
i already managed to send me meetings with the vCalendar standard used in Outlook:
BEGIN: VEVENT
…
END: VEVENT
As in Outlook 2003 the VTODO does not exist, I don't know how to continue.
Can anybody help me? Do I need to install someting particular on the webserver that I can make use of the Outlook Object with PHP?
Regards
Pascal
January 17th, 2008 at 10:00 pm
very helpfull !!
I'm working on it, I might publish some more examples when I handle.
February 2nd, 2008 at 5:05 am
Hi, please help on this,
Unable to connect to Outlook: Failed to create COM object `Outlook.Application': Call was rejected by callee.
Would appreciate very much for helping. Thanks
February 16th, 2008 at 10:14 pm
This is great but can one do something fairly easy like loop through the tasks already present in Outlook and display them as a list on a webpage?
February 26th, 2009 at 9:02 am
Hi,
This script seem don't word with outlook 2003 / 2007.
Someone can help me ?
Thanks.