DEV Community

Code Salley
Code Salley

Posted on

Ruby on Rails event invitation add to calendar using icalendar gem

In rails sending an event invitation is simple, But getting an invite recognized by Gmail for easy "add to calendar" feature is tricky. After experimenting with it for a while, this simple config does the trick.

require 'icalendar/tzinfo'
class EventMailer < ApplicationMailer
 def send_invitation(_user, _event)

  # initialize a new icalendar class
  ical =

  # Define default time
  time_zone = 'UTC'
  cal_tz = TZInfo::Timezone.get time_zone

  # add timezone to icalendar 
  ical.add_timezone cal_tz

  # new icalendar event 
  event =

  # event start date
  event.dtstart = _event.start_time, 'tzid' => cal_tz

  # event end date
  event.dtend = _event.end_time, 'tzid' => cal_tz

  # event organizer
  event.organizer ="mailto:" +

  # event created date
  event.created =

  # event location
  event.location =  _event.venue

  # if there's an external link e.g, google meet
  event.uid = event.url = _event.meeting_link

  # event title
  event.summary = _event.title

  # event description
  event.description = _event.description

  # attach the configured event to icalendar class

  # protocol
  ical.append_custom_property("METHOD", "REQUEST")

  # this add's an attachment name `event.ics`, 
  # when clicked, the event gets added to the calendar. 

  mail.attachments['event.ics'] = { mime_type: 'application/ics', content: ical.to_ical }

  # send mail
  mail(to:, subject:  "#{_event.title} - #{_event.summary} ")
Enter fullscreen mode Exit fullscreen mode


Discussion (0)