How to create ICS in rails and send it as a attchment in a mail?
This can be done using the ri_cal gem: In order to create an event ics file you want to create the event:
event = RiCal.Event do
description "MA-6 First US Manned Spaceflight"
dtstart DateTime.parse("2/20/1962 14:47:39")
dtend DateTime.parse("2/20/1962 19:43:02")
location "Cape Canaveral"
add_attendee "[email protected]"
alarm do
description "Segment 51"
end
end
Then you use .export(stream)
on the event (this will insert the event to a wrapper calendar that contains only this event, so you don't have to wrap it yourself).
The stream can be set to a file that can be attached the way Andy suggested or you can call this method without a stream argument which will return a string that can be put into the attachment as is. That will look something like this:
class UserMailer < ActionMailer::Base
def send_event_email(user, event)
attachments['event.ics'] = event.export()
mail(:to => user.email, :subject => "Calendar event!")
end
end
With icalendar
Add this gem to your Gemfile
gem 'mail'
gem 'icalendar'
You must config mail gem inside config/enviroment.rb
for example for RoR >= 4.2
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
# Initialize sendgrid
ActionMailer::Base.smtp_settings = {
:user_name => 'username',
:password => 'password',
:domain => 'something.com',
:address => 'smtp.something.com',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
User model
has_may :calendar_events
Fields
- fullname
CalendarEvent model
belongs_to :user
Fields
- title
- description
- start_time
- end_time
- user_id
app/mailers/mail_notifier.rb
class MailNotifier < ActionMailer::Base
default from: '[email protected]'
def send_calendar_event(calendar_event, organizer)
@cal = Icalendar::Calendar.new
@cal.event do |e|
e.dtstart = calendar_event.start_time
e.dtend = calendar_event.end_time
e.summary = calendar_event.title
e.organizer = "mailto:#{organizer.mail}"
e.organizer = Icalendar::Values::CalAddress.new("mailto:#{organizer.mail}", cn: organizer.fullname)
e.description = calendar_event.description
end
mail.attachments['calendar_event.ics'] = { mime_type: 'text/calendar', content: @cal.to_ical }
mail(to: calendar_event.user.mail,
subject: "[SUB] #{calendar_event.description} from #{l(calendar_event.start_time, format: :default)}")
end
end
Now you can call MailNotifier from controller with the following code
MailNotifier.send_calendar_event(@calendar_event, organizer_user).deliver
Using ActionMailer
(API documentation), simply generate the file and add it to attachments
:
class ApplicationMailer < ActionMailer::Base
def send_ics(recipient)
attachments['event.ics'] = File.read('path/to/event.ics')
mail(:to => recipient, :subject => "Calendar event!")
end
end
You can do this without actually saving a file to the file system, but I'll leave this exercise up to you.
© 2022 - 2024 — McMap. All rights reserved.