我的应用需要向使用.ics附件的用户发送电子邮件。
目前,当用户点击网页上的链接时,我有一个呈现.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_to 'Download .ics file with right click', invite_path(format: :ics) %>
是否可以以相同的方式为电子邮件提供ics附件而无需先创建/保存文件然后再引用该路径?
如果是这样,我该如何做呢?
答案 0 :(得分:1)
您应该能够使用邮件附件发送文件。将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