Skip to content

Instantly share code, notes, and snippets.

@flandrade
Last active April 18, 2024 21:46
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flandrade/394d9df95e1be2a584f267ae91505239 to your computer and use it in GitHub Desktop.
Save flandrade/394d9df95e1be2a584f267ae91505239 to your computer and use it in GitHub Desktop.
Automatically Add iCalendar Events (.ics) to Calendars

Automatically Add iCalendar Events (.ics) to Calendars

Do you want to create a calendar event so that you can display it on an iPhone's calendar app or in Google Calendar? This can be done by using iCalendar events RFC 5545 as part of the following workflow:

  1. The user signs up for an event online.
  2. The user receives the iCalendar file as an attachment in the email.
  3. The .ics file contains information for the event at a specific time and date.

These events can be automatically added to any calendar if the file contains the appropriate information. Here you can find a quick guide to generate a valid iCalendar file for this purpose.

Attachment Headers

In order to get the email client to parse correctly the attached .ics file, you should add the scheduling method and MIME information to the email headers. This is specified by the iCalendar Message-Based Interoperability Protocol (RFC 2447).

For this reason, your header should include Content-Type, Content-Transfer-Encoding and Content-Disposition as specified in the following example:

Content-Type: text/calendar; charset=utf-8; method=REQUEST; name=invite.ics'
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=invite.ics

iCalendar Method

When used in a MIME message entity, the value of "METHOD" must be the same as the Content-Type "method". This can only appear once within the iCalendar object. The value of this field is defined by the iCalendar Transport-Independent Interoperability Protocol(iTIP - RFC 5546). In order to request for a meeting, the value should be "REQUEST".

"REQUEST" is used to make a request for an event. This is an explicit invitation to one or more attendees.

METHOD:REQUEST

iCalendar VEVENT

When creating the iCalendar Event, you should consider the required properties to add events automatically. These properties and parameters should be specified in the "VEVENT" calendar component.

ATTENDEE

This property is the state of a particular "Attendee" relative to an event. It is used for scheduling and is defined by the "PARTSTAT" parameter in the "ATTENDEE" property for each attendee.

ATTENDEE;PARTSTAT=ACCEPTED;CN="Jane 
 Doe";EMAIL=jdoe@gmail.com:MAILTO:jdoe@gmailcom

PARTSTAT

It specifies the status of the attendee's participation. The value might be one of the following:

  • NEEDS-ACTION: Event needs action. The event will be shown in the user's calendar but without an answer.
  • ACCEPTED: Event accepted. The event will be shown on the calendar as accepted.
  • DECLINED: Event declined. The event will show on the calendar as declined.
  • TENTATIVE: Event tentatively accepted. The event will be shown on the calendar with "MAYBE".
  • DELEGATED:  Event delegated. The event will be shown in the user's calendar but without an answer.

CN and EMAIL

These properties are the displayable name associated with the calendar address and the email address of the attendee. Email addresses could be specified as a mailto.

Here are more considerations regarding the "Attendee" property.

Final Example

To wrap up, here is a complete example of a valid .ics file that will be added automatically to your calendar. For more examples, please review the examples from the RFC 2447.

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
UID:<unique-id>@<site>.com
DTSTAMP:20210605T073803Z
DTSTART;TZID=America/Guayaquil:20210614T030000
DTEND;TZID=America/Guayaquil:20210614T040000
SUMMARY:My Event
ORGANIZER;CN="Juan Perez":mailto:jperez@organizer.com
ATTENDEE;PARTSTAT=ACCEPTED;CN="Jane 
 Doe";EMAIL=jdoe@gmail.com:MAILTO:jdoe@gmailcom
URL;VALUE=URI:https://<site>.com/event/5960492994476830083
END:VEVENT
END:VCALENDAR
@flandrade
Copy link
Author

flandrade commented Jun 20, 2021

Examples of how these events will appear in Gmail:

PARTSTAT=ACCEPTED
calendar-01

PARTSTAT=TENTATIVE
Screen Shot 2021-06-10 at 12 31 50 AM

@pawansingh00
Copy link

Thanks for adding this detailed info.

It works well for Gmail, but doesn't seems to be working for Outlook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment