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 :)

Tagged with:
 

47 Responses to PHP ICalendar Email Code

  1. [...] viaPHP ICalendar Email Code | Julian Young Marketing. [...]

    • Sean says:

      To update an iCal, you must have the same UID, and you must increment the SEQUENCE value as well. You may also be required to increment the LAST-MODIFIED field as well, but can’t vouch for that. In my initial tests, a unix timestamp works as the sequence, as I was generating ical events on the fly, so every time I created a new event, the sequence was larger by virtue of the timestamp. I have not tested to ensure this is accepted by all (or most) clients, but I did test with the Lightening plugin for Mozilla Thunderbird.

      Hope that helps.

  2. Lisa says:

    Hello,
    I found your code, it is really great that I was looking for.

    I have success it, but I still have problem.

    I don’t know how to update email after they accept that event.

    I am using this code from web page using sql data base.

    If you could, help me please, please cherry on top.

    Thank you.

    Lisa

  3. theneos says:

    Hi, i tried to use this..but running your sample code does not work on google/hotmail/outlook.

    i tried to make the METHOD to REQUEST and it worked on hotmail and google already..but on outlook it does not recognize the attachment..

    so i tried to change the header from multipart/alternative to multipart/mixed and outlook now can see the attachment which i can open add to my calendar but is there a way where i can see right away the Accept, Deny Tentative buttons?

    thanks

  4. theneos says:

    changed again to multipart/alternative and the problem with outlook not showing the buttons is that need to add charset on this line

    $message .= ‘Content-Type: text/calendar;name=”meeting.ics”;method=REQUEST;charset=utf-8\n’;

    thanks!

  5. Jonathan says:

    Anyone knows how to use this with authenticated SMTP?

  6. Claus says:

    This works great, thanks..

    Could someone please help me with converting it to use correct timezone.

    e.g. UTC + 1.00

  7. Safford says:

    This code, with theneos’ charset modification, worked well. I’m getting Accept/Decline/Tentative buttons in Outlook.

    What should I change in the code to have these buttons send a response back to me? Right now, when I click “Accept”, the meeting appears on my calendar, but no response is sent.

  8. Jaimin says:

    i dont know why.. i tried a lot with this script but it does not work.. i m using smtp to send mail. can u help..

  9. Jaimin says:

    I get this kind of reply in my mail…

    ——Meeting Booking—-f780f4ef534b83675175480963020b86 text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

    Dear Jaimin Panchal,

    Here is my HTML Email / Used for Meeting Description
    ——Meeting Booking—-f780f4ef534b83675175480963020b86 text/calendar;name=”meeting.ics”;method=REQUEST\nContent-Transfer-Encoding: 8bit BEGIN:VCALENDAR\r\n PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN\r\n VERSION:2.0\r\n METHOD:PUBLISH\r\n BEGIN:VEVENT\r\n ORGANIZER:MAILTO:abc@example.com\r\n DTSTART:20100706T134000Z\r\n DTEND:20100706T144000Z\r\n LOCATION:My Office\r\n TRANSP:OPAQUE\r\n SEQUENCE:0\r\n UID:20101215T111338-1451318093@example.com\r\n DTSTAMP:20101215T054338Z\r\n DESCRIPTION:Here is a brief description of my meeting \r\n SUMMARY:Meeting Booking\r\n PRIORITY:5\r\n CLASS:PUBLIC\r\n END:VEVENT\r\n END:VCALENDAR\r\n

    • Julian Young says:

      Hi Jaimin, without getting kneedeep in your code it’s hard to see what’s going wrong. At a glance it looks like your multipart seperation isn’t working. I would recommend you simplify as much as possible to discover the problem.

  10. Jaimin says:

    hey thanks for your reply.. your format helped me a lot. finally did it..

    To automatically send reply on accepting the meeting.

    ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;
    CN=name@example.com:MAILTO:name@example.com

    this will help to understand ATTENDEE : http://www.kanzaki.com/docs/ical/attendee.html

  11. recepti says:

    You are a very smart person! :)

  12. theneos says:

    This code really helped me a lot in my other project..thanks again!

  13. samu says:

    Hi,

    i cant found a ics file in my mail by using your code. help me out.

    thanks in advance

  14. Manoj says:

    I am getting same code as jaimin,
    but in my case, I am using the php.mailer.php class to send the mail
    thanks in advance….
    i kept the downloaded code as it is and used the above class instead the mail function of php

    • Julian Young says:

      Hi Manoj, I’m not sure how Jamain got round his issue. I’d recommend downloading the source and not copying and pasting to start you off. Other than that make sure there are no tabs or extra spaces in the $ical string.

  15. Manoj says:

    can you please help to figure out whats wrong in this code…..

    ——Meeting Booking—-22a76ff52a17573fe533afbe226e2f7c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
    Dear Manoj Deshmukh,

    Here is my HTML Email / Used for Meeting Description

    ——Meeting Booking—-22a76ff52a17573fe533afbe226e2f7c Content-Type: text/calendar;name=”meeting.ics”;method=REQUEST\nContent-Transfer-Encoding: 8bit BEGIN:VCALENDAR PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN VERSION:2.0 METHOD:PUBLISH BEGIN:VEVENT ORGANIZER:MAILTO:manoj.deshmukh@extentia.com DTSTART:20110112T184000Z DTEND:20110112T194000Z LOCATION:My Office TRANSP:OPAQUE SEQUENCE:0 UID:20110113T051628-5941@mydomain.com DTSTAMP:20110113T051628Z DESCRIPTION:Here is a brief description of my meeting SUMMARY:Meeting Booking PRIORITY:5 CLASS:PUBLIC END:VEVENT END:VCALENDAR

  16. Manoj says:

    Hi Julian,
    thanks for your comment…
    its working by using php mail function directly instead of php.mailer class.
    but now request not getting in my calender, no buttons (accept, reject, tentative)
    appearing in the mail.

  17. Stephanie says:

    Hi Julian,

    Another confused would-be user here. I’ve downloaded your .zip file and have tried only changing the email address so it sends the test to me. However, the email that appears in my inbox only has the message content and nothing to do with the calendar appears (no attatchment, no buttons, just a simple Dear John letter. Very frustrating as I know this works for other people, and am not sure why it is not working for me! Thanks for your help in advance!

  18. Stephanie says:

    To add – when opening the email on my iPod Touch the calendar attatchment shows and can be added to my calendar, also on Outlook 2007 it seems to work fine – just a problem with Outlook 2003?

  19. Joseph Sliker says:

    I suspect I will be buying you a beer shortly as soon as I see how well I can get this to work with the in-house calendar I’ve been monkeying with for the past several years. The basics of it seemed to work for me right out of the box! God bless you for putting this together.

    • Julian Young says:

      Thanks Joseph, that’ll be my first beer so I hope it works out for you. Please keep us posted on your progress :)

    • Julian Young says:

      Thanks for the beers Jospeh, I finally had an opportunity to drink them the other night. It means a lot. Cheers!

  20. Ali says:

    Really appreciate your efforts Julian! thank you.

    I also wanted to know if there is a way that I can make the ‘proposed new time’ feature work in Outlook 07. I tried doing it but wasn’t really working how its supposed to.

    Thanks in Advance,
    Ali

  21. JAI says:

    Hi
    I have downloded ur code and tried to send mail ,I cant able to see ACCEPT/REJECT BUTTON in gmail and I cant able to view the calender in outlook. Please help me out.

  22. Holodyn says:

    Julian – wonderful code.

    Had to make some changes for the event to import on Outlook 2007

    iCal Data Changed from:
    METHOD:PUBLISH

    to:
    METHOD:REQUEST

    iCal Data Added:
    UID:’.date(‘Ymd’).’T’.date(‘His’).’-’.rand().’-example.com

    The UID should be coded to relate to an event record so future changes can be synchronized

  23. Chris says:

    Hi got this all working but any ideas on how to construct a cancel event email.

    got this so far but not working

    $ical = ‘BEGIN:VCALENDAR
    PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
    VERSION:2.0
    BEGIN:VEVENT
    METHOD:CANCEL
    STATUS:CANCELLED
    ORGANIZER:MAILTO:bookings@company.com
    DTSTART:’.$dtstart.’
    DTEND:’.$dtend.’
    LOCATION:’.$RESdetails[room].’
    TRANSP:OPAQUE
    SEQUENCE:1
    UID:’.$cal_uid.’
    DTSTAMP:’.$todaystamp.’
    DESCRIPTION:IT Room Bookings
    SUMMARY:Room Booking
    PRIORITY:5
    CLASS:PUBLIC
    END:VEVENT
    END:VCALENDAR’;

    $message .= ‘Content-Type: text/calendar;name=”meeting.ics”;method=CANCEL;charset=utf-8\n’;
    $message .= “Content-Transfer-Encoding: 8bit\n\n”;
    $message .= $ical;

  24. rajan says:

    Hi Julian ,
    Recently i have implemented your code.In outlook it is working fine .
    In gmail, i am not able to get the meeting content.

    How can we compose “Yes”, “No”,”May be” content in gmail . so that user can respond from gmail to update their status.

    I tried with old uid number(already created appointemnt google calendar).This scenario i can receive the message with “Yes”, “No”,”May be”.

    Could you guide me how can i implement new appointment request in gmail calendar? (generating uid for new calendar)

  25. Just what I needed, implementing in CodeIgniter now. Thanks a lot.

  26. hammand says:

    I want to achieve the same but using SMTP. Invitation should come as an actual calendar invitation with the text in the body of the email not as an attachment in an email.

  27. Steph says:

    Hi,

    didn´t have any ideas to get the iCal can be responsed for cancelation or acception.

    My problem is that if i cancel the invitation, the sender isn´t informed by the invited person.

    Are there any solutions?

    Testing on a Outlook Web Access

    Thx for replys,
    Steph

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...