Issue including calendar attachment in Mandrill Mailer and Rails

左心房为你撑大大i 提交于 2019-12-12 02:24:03

问题


I'm currently using the icalendar gem to create a new ical calendar and then send it via the mandrill_mailer gem as an attachment. I've tried a variety of different methods - so far I believe I've gotten closest with:

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

I know my mailer stuff is set up correctly since I'm able to send other types of transactional emails successfully. Also, according to this S.O. post I can't send it directly as a .ics file which is why I'm sending the base64 encoded version of it. Here is the error I keep getting regardless of what I do (whether it's the above or creating a tmp file and opening/reading the newly created tmp file in calendar_mailer.rb):

TypeError: no implicit conversion of nil into String from /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:inencode64' 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: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:inmap' 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 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:inmandrill_mail' from /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:incall' 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 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:instart' from /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:inconsole' 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: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' from bin/rails:4:in `require'

Thanks in advance.


回答1:


Probably not the best code in the world, but an example:

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

And

ics_file      = Outlook.create_cal

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


来源:https://stackoverflow.com/questions/31641657/issue-including-calendar-attachment-in-mandrill-mailer-and-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!