如何在Action Mailer(Rails 4.0.1)中设置内容类型?

时间:2013-11-27 21:47:52

标签: ruby-on-rails actionmailer

我正在尝试通过铁路邮件发送ics日历请求。我被困在如何将日历请求输入到电子邮件中,以便Outlook将显示接受/拒绝/可能提示。这是到目前为止邮件的代码。

class CalendarMailer < ActionMailer::Base
  default from: "system@time-tracker.com",
          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

其中文件由以下代码块生成

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

我的第一个问题是如何在Rails 4中设置邮件对象的内容/类型?你会想到我使用的三种方法之一,轨道会弄清楚我希望它是'text / Calendar'而不是'text / plain'。我不确定是否还有其他任何关于发送日历邀请的最终目标的问题,因为我还没有解决内容类型问题。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

似乎是ActionMailer中的一个错误。这是我目前正在使用的俗气解决方法。

旧代码:

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

现在:

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