Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Adding Events to Microsoft Outlook from PHP using ICAL

David Carr

PHP & MySQL Tutorials

    I've recently been trying to find the best way of creating a calendar event outlook from a php driven application.

    At first I looked into way of communication with the exchange server directly I soon realized this wasn't going to be an easy route, I would need to use another protocol to do this something like CalDEV or similar would be needed.

    For my needs I didn't need the calendar to by synced only to add events when created so I decided to issue a request from php to outlook via an ics file that would be generated when requested. This approach is really simple to do. When the file is created I could either attach it to an email or server it directly from the browser, I chose the latter option.

    As I will be creating the file dynamically only when its requested I would need to send a few parameters to a php script which will create the file.

    The parameters are as follows:

    date         = date of event
    startTime = time of event only the hour and minutes
    endTime  = time event finished only the hour and minutes
    subject    = subject line of the event
    desc        = option for more details

    Using these parameters I simply wrap them with their values in a link to a script that will create the ics file.

    <a href="ical.php?date=20120302&amp;startTime=1300&amp;endTime=1400&amp;subject=Meeting&amp;desc=Meeting to discuss processes.">Add Appointment to your Outlook Calendar</a>

    Here's the contents of my icap.php file:

      $date      = $_GET['date'];
      $startTime = $_GET['startTime'];
      $endTime   = $_GET['endTime'];
      $subject   = $_GET['subject'];
      $desc      = $_GET['desc'];
      $ical = "BEGIN:VCALENDAR
    PRODID:-//hacksw/handcal//NONSGML v1.0//EN
    UID:" . md5(uniqid(mt_rand(), true)) . "
    DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
      //set correct content-type-header
      header('Content-type: text/calendar; charset=utf-8');
      header('Content-Disposition: inline; filename=calendar.ics');
      echo $ical;

    The date needs to be in the format of YYMMDD so remove any spaces or - from the date also I chose not to use seconds in the time and have them in the script, you can easily have them passed in the link as well just remove the 00 from DTSTART and DTEND.

    When this script is called it collected the parameters and outputs a newly generated calendar.ics file open that with outlook and you'll have the option to save the event.

    This route is perfect for letting the end user decide if they want to add the event or not.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.