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 = Icalendar::Calendar.new
# 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 = Icalendar::Event.new
# event start date
event.dtstart = Icalendar::Values::DateTime.new _event.start_time, 'tzid' => cal_tz
# event end date
event.dtend = Icalendar::Values::DateTime.new _event.end_time, 'tzid' => cal_tz
# event organizer
event.organizer = Icalendar::Values::CalAddress.new("mailto:" + _user.email)
# event created date
event.created = DateTime.now
# 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
ical.add_event(event)
# 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: _user.email, subject: "#{_event.title} - #{_event.summary} ")
end
end
enjoy!
Top comments (0)