Search code examples
phpicalendar

Creating ICS for Outlook Meeting Event using PHP


Attempting to generate .ics from php, unsuccessfully thus far. The file is created (it downloads to my /downloads folder) but when I click on the downloaded ics event, nothing happens, no popup in Outlook. Here's what I tried:

$company='company';//$_GET['company'];
$proj_title='projtitle';//$_GET['proj_title'];
$proj_num='project number';//$_GET['proj_num'];
$subject='subject';//$_GET['subject'];
$subject=$proj_num.' '.$company.' - '.$proj_title.': '.$subject;

$http='http%3A%2F%2F';
$fwd_slash='%2F';
$question_mark='%3F';
$equals='%3D';
$ampersand='%26';

$description='Brief description of meeting.';

/* begin ical */
$meeting_date=date('Y-m-d')." 09:00:00";
$meeting_duration=3600;

$meeting_stamp=strtotime($meeting_date." UTC");
$dtstart=gmdate("Ymd\THis\Z",$meeting_stamp);
$dtend=gmdate("Ymd\THis\Z",$meeting_stamp+$meeting_duration);

/* set up the event properties */
$event=array(
    'id'          => $proj_num,
    'subject'     => $subject,
    'description' => $description,
    'datestart'   => $dtstart,
    'dateend'     => $dtend,
    'location'    => 'Boardroom'
);

/* build the ics file */
$ical='BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTEND;TZID="America/New_York":'.$dtend.'
UID:'.md5($event['subject']).'
DTSTAMP:'.time().'
LOCATION:'.addslashes($event['location']).'
DESCRIPTION:'.addslashes($event['description']).'
URL;VALUE=URI:http://tracker/ics/'.$event['id'].'
SUMMARY:'.addslashes($event['subject']) . '
BEGIN:VALARM
TRIGGER:-PT30M
REPEAT:1
DURATION:PT15M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM'.'
DTSTART;TZID="America/New_York":'.$dtstart.'
END:VEVENT
END:VCALENDAR';

/* set correct content-type-header */
if($event['id']){
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=tracker_event.ics');
    echo $ical;
}
else{
    header('Location: /');
}

I am relatively new to icalendar events and such, exhaustively researched online to come up with the code presented above, had some mild success using dummy values, until now I need to substitute with real data.

Any help is greatly appreciated.


Solution

  • Solved. Problem was me adding HTML content to the DESCRIPTION part of the ICS. I removed DESCRIPTION completely, and replaced it with:

    X-ALT-DESC;FMTTYPE=text/html:<html>'.$html.'</html>
    

    Where $html stores the content you wish to have displayed (in Outlook 2016, my deployment environment) as HTML-formatted content.

    And now everything works as I needed it to. Hope this helps someone one day.