PHP ICalendar Email Code

Working diligently day and night  (oh, the commitment) to tackle PHP iCal emails, I finally got some code working. I was amazed at how few examples I could find, the majority of which would not work. The single biggest problem with getting this working was making those Accept, Deny, Tentative buttons appear in Outlook.

My Largest issue with coding this was the following message….

As the meeting organizer, you do not need to respond to the meeting

Lies! This was caused by the actual headers and encoding of the email rather than the content of my iCal information. Anyway, after 2 days (and evenings) of trying to get this working I finally broke through at 3pm today!

The Code

The following code has been genericised to an extent, if you have any problems getting it working please let me know. It does not generate a reponse when buttons are pressed as it was designed for use from a noreply email address; in order to get a response you would need to update the iCal code. You may prefer to download this with the example rather than copying and pasting, the iCal code is very sensitive to extra characters, tabs, etc.

//Written Using PHP4, it will probably work fine in PHP5. I tested using Outlook 2003/ 2007, Exchange Server 2003, Googlemail, Hotmail and Google Calendar.
//$firstname is the first name of target
//$lastname is the last name of target
//$email is the targets email address
//$meeting_date is straight from a DATETIME mysql field and assumes UTC.
//$meeting_name is the name of your meeting
//$meeting_duration is the duration of your meeting in seconds (3600 = 1 hour)

function sendIcalEmail ($firstname, $lastname, $email, $meeting_date, $meeting_name, $meeting_duration)  {

	$from_name = “My Name”;
	$from_address = “myname@mydomain.com”;
	$subject = “Meeting Booking”; //Doubles as email subject and meeting subject in calendar
	$meeting_description = “Here is a brief description of my meeting\n\n”;
	$meeting_location = “My Office”; //Where will your meeting take place

	//Convert MYSQL datetime and construct iCal start, end and issue dates
	$meetingstamp = strtotime($meeting_date . ” UTC”);
	$dtstart= gmdate(”Ymd\THis\Z”,$meetingstamp);
	$dtend= gmdate(”Ymd\THis\Z”,$meetingstamp+$meeting_duration);
	$todaystamp = gmdate(”Ymd\THis\Z”);

	//Create unique identifier
	$cal_uid = date(’Ymd’).’T’.date(’His’).”-”.rand().”@mydomain.com”;

	//Create Mime Boundry
	$mime_boundary = “—-Meeting Booking—-”.md5(time());

	//Create Email Headers
	$headers = “From: “.$from_name.” <”.$from_address.”>\n”;
	$headers .= “Reply-To: “.$from_name.” <”.$from_address.”>\n”;

	$headers .= “MIME-Version: 1.0\n”;
	$headers .= “Content-Type: multipart/alternative; boundary=\”$mime_boundary\”\n”;
	$headers .= “Content-class: urn:content-classes:calendarmessage\n”;

	//Create Email Body (HTML)
	$message .= “–$mime_boundary\n”;
	$message .= “Content-Type: text/html; charset=UTF-8\n”;
	$message .= “Content-Transfer-Encoding: 8bit\n\n”;

	$message .= “<html>\n”;
	$message .= “<body>\n”;
	$message .= ‘<p>Dear ‘.$firstname.’ ‘.$lastname.’,</p>’;
	$message .= ‘<p>Here is my HTML Email / Used for Meeting Description</p>’;
	$message .= “</body>\n”;
	$message .= “</html>\n”;
	$message .= “–$mime_boundary\n”;

	//Create ICAL Content (Google rfc 2445 for details and examples of usage, beware of adding tabs)
	$ical =    ‘BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
VERSION:2.0
METHOD:PUBLISH
BEGIN:VEVENT
ORGANIZER:MAILTO:’.$from_address.’
DTSTART:’.$dtstart.’
DTEND:’.$dtend.’
LOCATION:’.$meeting_location.’
TRANSP:OPAQUE
SEQUENCE:0
UID:’.$cal_uid.’
DTSTAMP:’.$todaystamp.’
DESCRIPTION:’.$meeting_description.’
SUMMARY:’.$subject.’
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR’;

	$message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST;charset=utf-8\n';
	$message .= 'Content-Transfer-Encoding: 8bit\n\n';
	$message .= $ical;

	//SEND MAIL
	$mail_sent = @mail( $email, $subject, $message, $headers );

	if($mail_sent)     {
		return true;
	} else {
		return false;
	}

}

Download Full Source and Example

Finally


Hopefully I will have saved you some time and this will be enough to get you kickstarted and developing some lovely ICAL PHP code. If you found this useful then please consider using paypal to buy me a beer by clicking the icon on the right! Thanks :)

77 replies
« Older Comments

Trackbacks & Pingbacks

« Older Comments

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *