bulletshot60
bulletshot60

Reputation: 165

How to set content-type in Action Mailer (Rails 4.0.1)?

I am attempting to send a ics calendar request via a rails mailer. I am stuck on how to input the calendar request into an email so Outlook will display the accept/decline/maybe prompt. This is the code for the mailer so far.

class CalendarMailer < ActionMailer::Base
  default from: "[email protected]",
          content_type: "text/calendar"

  def send_schedule_request(user, time_request)
    body = File.open("#{Rails.root}/public/schedule_requests/{time_request.id}.ics","r").read

    headers['Content-Type'] = "text/calendar"

    mail(to: user.email,
         body: body,
         content_type: "text/calendar",
         subject: "#{time_request.time_request_type ? time_request.time_request_type.name : ""} #{time_request.name}")
  end
end

where the file is generated by the following block of code

def create_calendar_event(event, creator, approvers) 
  File.open("#{Rails.root}/public/schedule_requests/#{event.id}.ics", "w") do |file|
    file.write "BEGIN:VCALENDAR\n"
    file.write "METHOD:REQUEST\n"
    file.write "PRODID:Microsoft Exchange Server 2010\n"
    file.write "VERSION:2.0\n"
    file.write "BEGIN:VTIMEZONE\n"
    file.write "TZID:Eastern Standard Time\n"
    file.write "BEGIN:STANDARD\n"
    file.write "DTSTART:16010101T020000\n"
    file.write "TZOFFSETFROM:-0400\n"
    file.write "TZOFFSETTO:-0500\n"
    file.write "RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11\n"
    file.write "END:STANDARD\n"
    file.write "BEGIN:DAYLIGHT\n"
    file.write "DTSTART:16010101T020000\n"
    file.write "TZOFFSETFROM:-0500\n"
    file.write "TZOFFSETTO:-0400\n"
    file.write "RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3\n"
    file.write "END:DAYLIGHT\n"
    file.write "END:VTIMEZONE\n"
    file.write "BEGIN:VEVENT\n"
    file.write "ORGANIZER;CN=#{creator.first_name} #{creator.last_name}:MAILTO:#{creator.email}\n"
    approvers.each do |approver|
      file.write "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE;CN=#{approver.first_name} #{approver.last_name}:MAILTO:#{approver.email}\n"
    end
    file.write "DESCRIPTION;LANGUAGE=en-US:#{event.comment}\n"
    file.write "SUMMARY;LANGUAGE=en-US:#{event.time_request_type ? event.time_request_type.name : "Event"} #{event.name}\n"
    file.write "DTSTART;TZID=Eastern Standard Time:#{event.from.strftime("%Y%m%dT%H%M%S")}\n"
    file.write "DTEND;TZID=Eastern Standard Time:#{event.to.strftime("%Y%m%dT%H%M%S")}\n"
    file.write "UID: time-tracker-generated-event-#{event.id}-#{creator.email}\n"
    file.write "CLASS:PUBLIC\n"
    file.write "PRIORITY:5\n"
    file.write "DTSTAMP:#{DateTime.now.strftime("%Y%m%dT%H%M%SZ")}\n"
    file.write "TRANSP:OPAQUE\n"
    file.write "STATUS:CONFIRMED\n"
    file.write "SEQUENCE:0\n"
    file.write "LOCATION;LANGUAGE=en-US:\n"
    file.write "X-MICROSOFT-CDO-APPT-SEQUENCE:0\n"
    file.write "X-MICROSOFT-CDO-OWNERAPPTID:-1531201571\n"
    file.write "X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE\n"
    file.write "X-MICROSOFT-CDO-INTENDEDSTATUS:FREE\n"
    file.write "X-MICROSOFT-CDO-ALLDAYEVENT:FALSE\n"
    file.write "X-MICROSOFT-CDO-IMPORTANCE:1\n"
    file.write "X-MICROSOFT-CDO-INSTTYPE:0\n"
    file.write "X-MICROSOFT-DISALLOW-COUNTER:FALSE\n"
    file.write "END:VEVENT\n"
    file.write "END:VCALENDAR\n"
  end
end

My first question is how do I set the content/type of a mail object in Rails 4? You would think between one of the three methods I am using that rails would figure out I want it to be 'text/Calendar' not 'text/plain'. I am not sure if there is anything else that I should ask about my final objective of sending a calendar invite since I have not gotten past the content-type problem. Any help is greatly appreciated.

Upvotes: 2

Views: 3791

Answers (1)

Michael Chaney
Michael Chaney

Reputation: 3041

Seems to be a bug in ActionMailer. Here's the cheesy workaround I'm currently using.

Old code:

headers['Content-Type'] = 'text/html'
mail to: to, from: from, subject: subject, body: body

Now:

mail to: to, from: from, subject: subject do |format|
  format.html { render html: body }
end

Upvotes: 2

Related Questions