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 Even…


This content originally appeared on DEV Community and was authored by Code Salley

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!


This content originally appeared on DEV Community and was authored by Code Salley


Print Share Comment Cite Upload Translate Updates
APA

Code Salley | Sciencx (2022-01-22T19:13:17+00:00) Ruby on Rails event invitation add to calendar using icalendar gem. Retrieved from https://www.scien.cx/2022/01/22/ruby-on-rails-event-invitation-add-to-calendar-using-icalendar-gem/

MLA
" » Ruby on Rails event invitation add to calendar using icalendar gem." Code Salley | Sciencx - Saturday January 22, 2022, https://www.scien.cx/2022/01/22/ruby-on-rails-event-invitation-add-to-calendar-using-icalendar-gem/
HARVARD
Code Salley | Sciencx Saturday January 22, 2022 » Ruby on Rails event invitation add to calendar using icalendar gem., viewed ,<https://www.scien.cx/2022/01/22/ruby-on-rails-event-invitation-add-to-calendar-using-icalendar-gem/>
VANCOUVER
Code Salley | Sciencx - » Ruby on Rails event invitation add to calendar using icalendar gem. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/22/ruby-on-rails-event-invitation-add-to-calendar-using-icalendar-gem/
CHICAGO
" » Ruby on Rails event invitation add to calendar using icalendar gem." Code Salley | Sciencx - Accessed . https://www.scien.cx/2022/01/22/ruby-on-rails-event-invitation-add-to-calendar-using-icalendar-gem/
IEEE
" » Ruby on Rails event invitation add to calendar using icalendar gem." Code Salley | Sciencx [Online]. Available: https://www.scien.cx/2022/01/22/ruby-on-rails-event-invitation-add-to-calendar-using-icalendar-gem/. [Accessed: ]
rf:citation
» Ruby on Rails event invitation add to calendar using icalendar gem | Code Salley | Sciencx | https://www.scien.cx/2022/01/22/ruby-on-rails-event-invitation-add-to-calendar-using-icalendar-gem/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.