问题,包括Mandrill Mailer和Rails中的日历附件 [英] Issue including calendar attachment in Mandrill Mailer and Rails
问题描述
我目前正在使用 icalendar 宝石创建新的ical日历,然后通过 mandrill_mailer gem 作为附件.我尝试了多种不同的方法-到目前为止,我相信我与以下方法最接近:
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
我知道我的邮件程序设置正确,因为我能够成功发送其他类型的交易电子邮件.此外,根据此S.O. post 我无法将其直接作为.ics文件发送,这就是为什么我要发送它的base64编码版本.无论执行什么操作(无论是上面的操作还是创建tmp文件并在calendar_mailer.rb中打开/读取新创建的tmp文件),这都是我不断收到的错误:
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:没有将nil隐式转换为String 来自/usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/base64.rb:38:
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:在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
地图中'' 来自/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:inmandrill_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:insend_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
call' 从/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:inmethod_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:在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:inrun_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:in'require'
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:in
encode64' 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:inblock 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' 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:inmandrill_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' from /Users/alansalganik/projects/glyfe/app/mailers/calendar_mailer.rb:8:insend_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
call' 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:inmethod_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' 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:instart' 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
console' 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:inrun_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'
谢谢.
推荐答案
可能不是世界上最好的代码,而是一个示例:
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
还有
ics_file = Outlook.create_cal
mandrill_mail(
(...)
attachments: [
{ content: ics_file, name: 'ical.ics', type: 'text/calendar' }
]
)
这篇关于问题,包括Mandrill Mailer和Rails中的日历附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!