如何在rails中创建ICS并将其作为邮件附件发送?

发布于 2024-10-12 14:34:58 字数 33 浏览 4 评论 0原文

如何在rails中创建ICS并将其作为邮件附件发送?

How to create ICS in rails and send it as a attchment in a mail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫嚣ゝ 2024-10-19 14:34:58

这可以使用 ri_cal gem 来完成:
为了创建您想要创建事件的事件 ics 文件:

event = RiCal.Event do
      description "MA-6 First US Manned Spaceflight"
      dtstart     DateTime.parse("2/20/1962 14:47:39")
      dtend       DateTime.parse("2/20/1962 19:43:02")
      location    "Cape Canaveral"
      add_attendee "[email protected]"
      alarm do
        description "Segment 51"
      end
    end

然后对事件使用 .export(stream) (这会将事件插入到仅包含此事件的包装日历中,因此您不必自己包装它)。
可以将流设置为可以按照 Andy 建议的方式附加的文件,或者您可以在不使用流参数的情况下调用此方法,该方法将返回一个可以按原样放入附件中的字符串。那看起来像这样:

class UserMailer < ActionMailer::Base
  def send_event_email(user, event)
    attachments['event.ics'] = event.export()
    mail(:to => user.email, :subject => "Calendar event!")
  end
end

This can be done using the ri_cal gem:
In order to create an event ics file you want to create the event:

event = RiCal.Event do
      description "MA-6 First US Manned Spaceflight"
      dtstart     DateTime.parse("2/20/1962 14:47:39")
      dtend       DateTime.parse("2/20/1962 19:43:02")
      location    "Cape Canaveral"
      add_attendee "[email protected]"
      alarm do
        description "Segment 51"
      end
    end

Then you use .export(stream) on the event (this will insert the event to a wrapper calendar that contains only this event, so you don't have to wrap it yourself).
The stream can be set to a file that can be attached the way Andy suggested or you can call this method without a stream argument which will return a string that can be put into the attachment as is. That will look something like this:

class UserMailer < ActionMailer::Base
  def send_event_email(user, event)
    attachments['event.ics'] = event.export()
    mail(:to => user.email, :subject => "Calendar event!")
  end
end
薄荷港 2024-10-19 14:34:58

使用 icalendar

将此 gem 添加到您的 Gemfile

gem 'mail'
gem 'icalendar'

您必须在内部配置邮件 gem < code>config/enviroment.rb 例如,RoR >= 4.2

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!

# Initialize sendgrid
ActionMailer::Base.smtp_settings = {
  :user_name => 'username',
  :password => 'password',
  :domain => 'something.com',
  :address => 'smtp.something.com',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

用户模型

has_may :calendar_events

字段

  • 全名
  • 邮件

CalendarEvent model

belongs_to :user

Fields

  • title
  • description
  • start_time
  • end_time
  • user_id

app/mailers/mail_notifier.rb

class MailNotifier < ActionMailer::Base
  default from: '[email protected]'
  def send_calendar_event(calendar_event, organizer)
    @cal = Icalendar::Calendar.new
    @cal.event do |e|
      e.dtstart = calendar_event.start_time
      e.dtend = calendar_event.end_time
      e.summary = calendar_event.title
      e.organizer = "mailto:#{organizer.mail}"
      e.organizer = Icalendar::Values::CalAddress.new("mailto:#{organizer.mail}", cn: organizer.fullname)
      e.description = calendar_event.description
    end
    mail.attachments['calendar_event.ics'] = { mime_type: 'text/calendar', content: @cal.to_ical }
    mail(to: calendar_event.user.mail,
    subject: "[SUB] #{calendar_event.description} from #{l(calendar_event.start_time, format: :default)}")
  end
end

现在您可以从以下位置调用 MailNotifier具有以下代码的控制器

MailNotifier.send_calendar_event(@calendar_event, organizer_user).deliver

With icalendar

Add this gem to your Gemfile

gem 'mail'
gem 'icalendar'

You must config mail gem inside config/enviroment.rb for example for RoR >= 4.2

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!

# Initialize sendgrid
ActionMailer::Base.smtp_settings = {
  :user_name => 'username',
  :password => 'password',
  :domain => 'something.com',
  :address => 'smtp.something.com',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

User model

has_may :calendar_events

Fields

  • fullname
  • mail

CalendarEvent model

belongs_to :user

Fields

  • title
  • description
  • start_time
  • end_time
  • user_id

app/mailers/mail_notifier.rb

class MailNotifier < ActionMailer::Base
  default from: '[email protected]'
  def send_calendar_event(calendar_event, organizer)
    @cal = Icalendar::Calendar.new
    @cal.event do |e|
      e.dtstart = calendar_event.start_time
      e.dtend = calendar_event.end_time
      e.summary = calendar_event.title
      e.organizer = "mailto:#{organizer.mail}"
      e.organizer = Icalendar::Values::CalAddress.new("mailto:#{organizer.mail}", cn: organizer.fullname)
      e.description = calendar_event.description
    end
    mail.attachments['calendar_event.ics'] = { mime_type: 'text/calendar', content: @cal.to_ical }
    mail(to: calendar_event.user.mail,
    subject: "[SUB] #{calendar_event.description} from #{l(calendar_event.start_time, format: :default)}")
  end
end

Now you can call MailNotifier from controller with the following code

MailNotifier.send_calendar_event(@calendar_event, organizer_user).deliver
八巷 2024-10-19 14:34:58

使用 ActionMailerAPI 文档),只需生成文件并将其添加到附件

class ApplicationMailer < ActionMailer::Base
  def send_ics(recipient)
    attachments['event.ics'] = File.read('path/to/event.ics')
    mail(:to => recipient, :subject => "Calendar event!")
  end
end

您可以在不实际将文件保存到文件系统的情况下执行此操作,但我将把这个练习留给您。

Using ActionMailer (API documentation), simply generate the file and add it to attachments:

class ApplicationMailer < ActionMailer::Base
  def send_ics(recipient)
    attachments['event.ics'] = File.read('path/to/event.ics')
    mail(:to => recipient, :subject => "Calendar event!")
  end
end

You can do this without actually saving a file to the file system, but I'll leave this exercise up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文