问题包括Mandrill Mailer和Rails中的日历附件

时间:2015-07-26 20:32:37

标签: ruby-on-rails icalendar mandrill

我目前正在使用icalendar gem创建新的日历,然后通过mandrill_mailer gem作为附件发送。我尝试了各种不同的方法 - 到目前为止我相信我最接近:

Event.rb

require 'base64'

def self.export_events(user)
    @event = Event.last
    @calendar = Icalendar::Calendar.new
    event = Icalendar::Event.new
    event.summary = @event.title
    event.dtstart = @event.start_time.strftime("%Y%m%dT%H%M%S")
    event.dtend = @event.end_time.strftime("%Y%m%dT%H%M%S")
    event.description = @event.desc
    event.location = @event.location
    @calendar.add_event(event)
    encoded_cal = Base64.encode64(@calendar.to_ical)
    CalendarMailer.send_to_ical(user, encoded_cal).deliver
  end

calendar_mailer.rb

class CalendarMailer < MandrillMailer::TemplateMailer
  default from: "blah@blah.com"

  # iCal
  def send_to_ical(user, encoded_cal)
    mandrill_mail template: "ical-file",
    subject: "Your iCal file",
    to: { email: user.email, name: user.name },
    inline_css: true,
    async: true,
    track_clicks: true,
    attachments: [
      {
        type: "text/calendar",
        content: encoded_cal,
        name: "calendar.ics",
      }
    ]
  end
end

我知道我的邮件设置正确,因为我能够成功发送其他类型的交易电子邮件。另外,根据this S.O. post,我无法直接将其作为.ics文件发送,这就是我发送它的base64编码版本的原因。以下是我不断做的错误(无论是上面的内容还是创建tmp文件并打开/读取calendar_mailer.rb中新创建的tmp文件):

  

TypeError:没有将nil隐式转换为String       来自/usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/base64.rb:38:in pack' from /usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/base64.rb:38:in encode64'       来自/usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/mandrill_mailer-0.4.13/lib/mandrill_mailer/core_mailer.rb:263:in block in mandrill_attachment_args' from /usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/mandrill_mailer-0.4.13/lib/mandrill_mailer/core_mailer.rb:258:in map'       来自/usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/mandrill_mailer-0.4.13/lib/mandrill_mailer/core_mailer.rb:258:in mandrill_attachment_args' from /usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/mandrill_mailer-0.4.13/lib/mandrill_mailer/template_mailer.rb:191:in mandrill_mail'       来自/Users/alansalganik/projects/glyfe/app/mailers/calendar_mailer.rb:8:in send_to_ical' from /usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/mandrill_mailer-0.4.13/lib/mandrill_mailer/core_mailer.rb:283:in来电'       来自/usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/mandrill_mailer-0.4.13/lib/mandrill_mailer/core_mailer.rb:283:in method_missing' from (irb):763 from /usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in start'       来自/usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in start' from /usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in安慰'       来自/usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in run_command!' from /usr/local/rvm/gems/ruby-2.0.0-p481@rails-4.0.2/gems/railties-4.1.1/lib/rails/commands.rb:17:in “       来自bin / rails:4:在`require'

提前致谢。

1 个答案:

答案 0 :(得分:0)

可能不是世界上最好的代码,而是一个例子:

class Outlook

  def self.create_cal
    @calendar = Icalendar::Calendar.new
    event = Icalendar::Event.new
    event.summary = "SUMMARY"
    event.dtstart = Time.now.strftime("%Y%m%dT%H%M%S")
    event.dtend = (Time.now + 1.hour).strftime("%Y%m%dT%H%M%S")
    event.description = "DESC"
    event.location = "Holborn, London WC1V"
    @calendar.add_event(event)
    return @calendar.to_ical
  end

end

ics_file      = Outlook.create_cal

mandrill_mail(
  (...)
  attachments: [
    { content: ics_file, name: 'ical.ics', type: 'text/calendar' }
  ]
 )