本文介绍了问题,包括Mandrill Mailer和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: "[email protected]"

  # 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):

谢谢.

推荐答案

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

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中的日历附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:32