2016-12-01 140 views
0

我的应用程序需要发送电子邮件给.ics附件的用户。可能通过电子邮件发送.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-attachment而不先创建/保存该文件,然后引用该路径?

如果是这样,我该如何去做这件事?

回答

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