Sending Meeting Requests with Rails and Action Mailer

As a part of our Recruitment Tracking Solution, we send interview appointments as meeting requests to integrate with various calendar solutions like Outlook, Apple iCal and Google Calendar.

If you want to send a meeting request through email, you can do it in two ways. First, you can directly mention the meeting date and time in the email body which people do it usually. Second, you can send a calendar request along with the email which looks like this

Isn’t it cool!!

To implement this in ruby on rails, you need icalendar gem, a ruby library for dealing with icalendar files having extension .ics. It’s pretty simple to implement. Add the following line in your gemfile

gem 'icalendar'

then run bundle

Suppose you have a mailer MeetingNotification, you would just need to add the format.ics block as mentioned below

class MeetingNotification < ActionMailer::Base
   def meeting_request_with_calendar
     mail(:to => "any_email@example.com", :subject => "iCalendar",
                  :from => "any_email@example.com") do |format|
       format.ics {
       ical = Icalendar::Calendar.new
       e = Icalendar::Event.new
       e.start = DateTime.now.utc
       e.start.icalendar_tzid="UTC" # set timezone as "UTC"
       e.end = (DateTime.now + 1.day).utc
       e.end.icalendar_tzid="UTC"
       e.organizer "any_email@example.com"
       e.uid "MeetingRequest#{unique_value}"
       e.summary "Scrum Meeting"
       e.description <<-EOF
         Venue: Office
         Date: 16 August 2011
         Time: 10 am
       EOF
       ical.add_event(e)
       i cal.publish
       ical.to_ical
       render :text => ical, :layout => false
      }
    end
  end
end

If you want to know little more about  how it happens, then lets delve into this. If you run the above code in rails console, you would see

It’s almost self explanatory. Although I would like to point out few things like the DTEND and DTSTART which has Z(indicates UTC) appended, it’s there because we have set the timezone to UTC.

iCalendar auto-generates UID with value as combination of date and time. But recently Google has updated it’s code and  it does not parse it properly. So you have to explicitly add UID . Also You should add unique value to e.uid like “MeetingRequest-#{unique_value}” inorder to distinguish between the request you send and update otherwise you might end up messing up the wrong request.


Follow

Get every new post delivered to your Inbox.