Add events to outlook calendar with php script

38,028

Solution 1

How your server application should be able to access a client application? You may send an email to your client with a calendar entry. Maybe this is slightly more comfortable for your user.

Solution 2

<?php
/**
 * @category   iCalendar
 * @description Basic code for sending an event invitation.
 * @version    1.0
*/

//Create ICAL Content (Google rfc 2445 for details and examples of usage) 
//reference : http://www.mavetju.org/programming/outlook-ics.php

$message="BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20110718T121000Z
DTEND:20110718T131000Z
DTSTAMP:20110525T075116Z
ORGANIZER;CN=From Name:mailto:from email id
UID:12345678
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP= TRUE;CN=Sample:mailto:[email protected]
DESCRIPTION:This is a test of iCalendar event invitation.
LOCATION: Kochi
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Test iCalendar
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR";

/*Setting the header part, this is important */
$headers = "From: From Name <From Mail>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/calendar; method=REQUEST;\n";
$headers .= '        charset="UTF-8"';
$headers .= "\n";
$headers .= "Content-Transfer-Encoding: 7bit";

/*mail content , attaching the ics detail in the mail as content*/
$subject = "Meeting Subject";
$subject = html_entity_decode($subject, ENT_QUOTES, 'UTF-8');

/*mail send*/
if(mail("To email", $subject, $message, $headers)) {

    echo "sent";
}else {
    echo "error";
}

?>

Solution 3

If you've not implemented it yet, CalDAV (http://caldav.calconnect.org/) provides calendaring extensions to WebDAV, if you need to add this functionality to your site. DAViCAL (http://www.davical.org/) appears to offer a solution to your problem but I've not used it so YMMV on it.

Solution 4

I played around with this and Outlook will automatically add it to the calendar if you send it as an email and the from address is the same email address as the account setup in Outlook. As soon as Outlook downloads the message it automatically adds it to the calendar.

Solution 5

I did this with PHP, basically creating an ical event inline on a separate php file that doesn't require any extra libraries for those of you still out there wanting to do it. Outlook/iCal event with PHP

Basically did it like this

echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "PRODID:-//YourSite//NONSGML YourSite//EN\n";
echo "METHOD:PUBLISH\n"; // required by Outlook
echo "BEGIN:VEVENT\n";
echo "UID:".date('Ymd').'T'.date('His')."-".rand()."-yoursite.com\n"; // required by Outlook
echo "DTSTAMP:".date('Ymd').'T'.date('His')."\n"; // required by Outlook
echo "DTSTART:$year"."$month"."$day"."T"."$time\n"; //20120824T093200 (Datetime format required) 
echo "SUMMARY:$summary\n";
echo "DESCRIPTION: this is just a test\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
Share:
38,028
shasi kanth
Author by

shasi kanth

I am a Web Developer, Husband, Father and Singer. [I am #SOreadytohelp] When not doing work, I would spend time with meditation and music. My Developer Story

Updated on March 18, 2020

Comments

  • shasi kanth
    shasi kanth about 4 years

    I want to add events to my outlook calendar from the php code. As outlook can accept a file of extension ".ics", I have tried this sample code to generate an ics file:

    <?php
    header("Content-Type: text/Calendar");
    header("Content-Disposition: inline; filename=calendar.ics");
    echo "BEGIN:VCALENDAR\n";
    echo "VERSION:2.0\n";
    echo "PRODID:www.testMeiCalendar.net\n";
    echo "METHOD:REQUEST\n"; // requied by Outlook
    echo "BEGIN:VEVENT\n";
    echo "DTSTART:20101231T230000\n";
    echo "DTEND:20110101T010000\n";
    echo "SUMMARY:New Years Eve Reminder\n";
    echo "LOCATION:Downtown\n";
    echo "DESCRIPTION:Let's get together for New Years Eve\n";
    echo "UID:ABCD1234\n";
    echo "SEQUENCE:0\n";
    echo "DTSTAMP:20101125T112600\n";
    echo "END:VEVENT\n";
    echo "END:VCALENDAR\n";
    ?>
    

    So now when I run this code in Firefox, I got a pop-up asking to open the generated ics file using the Microsoft Outlook and I opened it and saved it to outlook and finally an event is added in outlook.

    But is there a way I can automate this process? I mean, can I store the event in Outlook calendar directly from a php script, without needing to generate an ics file and saving it?

    • Albireo
      Albireo about 13 years
      You're aware appending an off-topic link like that to your question is spam?
    • shasi kanth
      shasi kanth about 13 years
      Oh.. Albiero, sorry.. i will remove that link. I thought to mention that link to have a look at the current scenario of me.
    • mob_web_dev
      mob_web_dev over 2 years
      @shasikanth I believe you got the solution. Can you help me how it solved ?
    • shasi kanth
      shasi kanth over 2 years
      @mob_web_dev Please refer to the below-accepted answer.. none worked for me!
  • shasi kanth
    shasi kanth about 13 years
    I feel there are 2 options yet. One is to use WEBDAV to add event from php to exchange server and then sync it with outlook. Second weird option is to upload the generated ics file to the Outlook calendar events folder in the pc. But thats weird. So looking at the first option.
  • shasi kanth
    shasi kanth about 13 years
    Can i add event to the exchange server using WEBDAV, and then synchronise the exchange server with the user's outlook.
  • DanielB
    DanielB about 13 years
    @dskanth Sry, that question I can't answer.
  • Cerin
    Cerin almost 10 years
    This might be difficult with some servers that validate the from address. e.g. If I try to send an email with my [email protected] account using "[email protected]", Gmail will refuse to send because it thinks I'm trying to forge an email. You'd ideally need the user's smtp credentials so you can send the email from their account.