可以在不创建 .ics 文件的情况下通过电子邮件发送它吗?

Possible to email an .ics file without creating it?

我的应用程序需要向用户发送带有 .ics 附件的电子邮件。

目前,当用户点击网页上的 link 时,我有一个呈现 .ics 文件的操作:

def invite
  cal = Icalendar::Calendar.new
  cal.event do |e|
    e.dtstart     = Icalendar::Values::Date.new('20050428')
    e.dtend       = Icalendar::Values::Date.new('20050429')
    e.summary     = "Meeting with the man."
    e.description = "Have a long lunch meeting and decide nothing..."
    e.ip_class    = "PRIVATE"
  end
  cal.publish
  render text: cal.to_ical 
end

Link:

<%= link_to 'Download .ics file with right click', invite_path(format: :ics) %>

是否可以以相同的方式为电子邮件提供 ics 附件,而无需先 creating/saving 文件然后引用路径?

如果是这样,我该怎么做?

您应该可以使用邮件附件发送文件。将 mime 类型设置为 text/calendar 并使用 .to_ical 作为文件内容。

cal 变量传递给邮件程序。

def invite
  cal = Icalendar::Calendar.new
  cal.event do |e|
    e.dtstart     = Icalendar::Values::Date.new('20050428')
    e.dtend       = Icalendar::Values::Date.new('20050429')
    e.summary     = "Meeting with the man."
    e.description = "Have a long lunch meeting and decide nothing..."
    e.ip_class    = "PRIVATE"
  end
  cal.publish
  InviteMailer.invite(current_user.email, cal).deliver_later # or .deliver_now
  render text: cal.to_ical
end

设置文件附件。

class InviteMailer < ApplicationMailer
  def invite(recipient, cal)
    mail.attachments['invite.ics'] = { mime_type: 'text/calendar', content: cal.to_ical }
    mail(to: recipient, subject: 'Invite')
  end
end

(我没有测试这个。)

http://api.rubyonrails.org/classes/ActionMailer/Base.html#class-ActionMailer%3a%3aBase-label-Attachments
http://guides.rubyonrails.org/action_mailer_basics.html