Probably everybody is familiar with the calendar event invite features?

If you use Microsoft Windows (with Outlook) or Mac OSX (with iCal) or just a basic browser with a GMail account and you use the calendar functionality, you would have realized, that every time you get invited or invite someone to an event, you get a basic email, that adds the recipient to the calendar event.

I had to write an invitation tool that was cross compatible between different platforms and integrated with SugarCRM for a customer of InsightfulCRM (Australian SugarCRM Gold Partner) where I work.

I started from some samples, and by reading the RFC of the protocol and the iCalendar Wikipedia entry.

According to the protocol RFC and the Wikipedia entry, the iCalendar and vCalendar seems really similar, so we can use the common items to create a meeting invite.

Let’s pick a basic sample so that we can run some tests on:

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20100616T080000Z
DTEND:20100616T090000Z
DTSTAMP:20100616T075116Z
ORGANIZER;CN=Enrico Simonetti:mailto:enrico@test.com
UID:12345678
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP= TRUE;CN=Sample:mailto:sample@test.com
DESCRIPTION:Complete event on http://www.sample.com/get_event.php?id=12345678
LOCATION: Sydney
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Test iCalendar
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

Now, how do we send this event?
It is pretty simple.
We need a specific set of headers on the email, and the content of the email has to be the structured meeting code itself.
(No, it is not an attachment, also if you see an attached file!)

The headers are exactly as below:

$headers = "From: Enrico <enrico@test.com>\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";

NB: be REALLY careful with the spacing. Apparently calendars are really really fragile, and if you only remove a space, it will result on Outlook removing the buttons at the top of the email itself, where you can accept/reject the meeting, or/and even the attendees will disappear from the list!
It really caused me a lot of headache!

Now we can actually send the above calendar invite, as body of the email, structuring the meeting as per above sample, inside the content of $message variable.

$subject = "Meeting Subject";
$subject = html_entity_decode($subject, ENT_QUOTES, 'UTF-8');
mail("sample@test.com", $subject, $message, $headers);

Of course you will have to send an email for each attendee.

There you go, you just invited your attendee to meeting with id ’12345678′!

As simple as that!