This topic is locked

Sending appointment data as .ics file attachement

2/4/2016 10:56:47 PM
PHPRunner Tips and Tricks
admin

As Wikipedia says:

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter-propose another meeting date/time.


Lets see if we can send such a file with appointment data attached to email that can be easily imported into Outlook or Google Calendar. Here is the sample code for AfterAdd event. We assume that we have a table with the following selfexplanatory fields: ID, NAME, DATE, ADDRESS, PHONE, EMAIL.
We assume that DATE field is a datetime field that contains appointment date and time. Appointment duration is one hour (DATE + 3600 seconds).



$event = array(

"id" => $values["ID"],

"subject" => "Appointment for ".$values["NAME"] ,

"description" => $values["NAME"]." ".$values["ADDRESS"]." ".$values["PHONE"],

"dtstart" => date("Y-m-d H:i:s", strtotime($values["DATE"])),

"dtend" => date("Y-m-d H:i:s", strtotime($values["DATE"]) + 3600),

"location" =>$values["ADDRESS"]

);
function dateToCal($time) {

return date("Ymd\THis\Z", strtotime($time));

}
// prepare .ics file

$ical = "BEGIN:VCALENDAR

VERSION:2.0

PRODID:-//hacksw/handcal//NONSGML v1.0//EN

CALSCALE:GREGORIAN

BEGIN:VEVENT

DTEND:" . dateToCal($event["dtend"]) . "

UID:" . md5($event["id"]) . "

DTSTAMP:" . $event["dtstart"] . "

LOCATION:" . addslashes($event["location"]) . "

DESCRIPTION:" . addslashes($event["description"]) . "

URL;VALUE=URI: http://mydomain.com/events/"; . $event["id"] . "

SUMMARY:" . addslashes($event["subject"]) . "

DTSTART:" .dateToCal($event["dtstart"]) . "

END:VEVENT

END:VCALENDAR";
// save it

runner_save_file($values["ADDRESS"].".ics", $ical);
// wait till file is saved

sleep(1);
// send an email

$email=$values["EMAIL"];

$subject="Hello, ".$values["NAME"];

$msg=$values["NAME"]." ".$values["ADDRESS"]." ".$values["PHONE"];

$attachments = array(array("path" => getabspath($values["ADDRESS"].".ics")));

$ret=runner_mail(array("to" => $email, "subject" => $subject, "body" => $msg, "attachments" => $attachments));
if(!$ret["mailed"])

echo $ret["message"];