Wednesday, April 17, 2013

Create and send calendar events to Outlook/iCal in PHP (ics Format)

Use the below code to create a calendar event in PHP and send it as email to Microsoft Outlook or iPhone Calendar.
 

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

//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:sample@test.com
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 \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";
}

? >

Popular Posts