Ruby on rails Rails 2.3.11 ActionMailer没有';当电子邮件有附件时,不能呈现html

Ruby on rails Rails 2.3.11 ActionMailer没有';当电子邮件有附件时,不能呈现html,ruby-on-rails,attachment,actionmailer,Ruby On Rails,Attachment,Actionmailer,我使用的是Rails 2.3.11。我使用以下方法创建了UserMailer: def rsvp_created(user, rsvp, pdf_file) setup_email(user) content_type "multipart/mixed" @subject << "Your RSVP for #{rsvp.ticket.holiday.title}" @body[:rsvp] = rsvp

我使用的是Rails 2.3.11。我使用以下方法创建了UserMailer:

     def rsvp_created(user, rsvp, pdf_file)
        setup_email(user)
        content_type "multipart/mixed"
        @subject << "Your RSVP for #{rsvp.ticket.holiday.title}"
        @body[:rsvp] = rsvp

        attachment :content_type => 'application/pdf', 
                   :body => File.read(pdf_file), 
                   :filename => "#{rsvp.confirmation_number}.pdf"
      end

      def rsvp_cancelled(user, rsvp)
        setup_email(user)
        content_type "text/html"
        @subject << "Cancelled RSVP for #{rsvp.ticket.holiday.title}"
        @body[:rsvp] = rsvp
        @body[:holiday_url] = APP_CONFIG['site_url'] + holiday_path(rsvp.ticket.holiday)
      end

protected
  def setup_email(user)
    @recipients = "#{user.email}"
    @from = APP_CONFIG['admin_email']
    @subject = "[#{APP_CONFIG['site_name']}] "
    @sent_on = Time.now
    @body[:user] = user
  end
def rsvp_已创建(用户、rsvp、pdf_文件)
设置\u电子邮件(用户)
内容类型“多部分/混合”
@主题“申请表/pdf”,
:body=>File.read(pdf_文件),
:filename=>“#{rsvp.confirmation_number}.pdf”
结束
def rsvp_已取消(用户,rsvp)
设置\u电子邮件(用户)
内容类型为“文本/html”

@subject使用Rails2.x,出于某种原因,您需要定义HTML显示的所有部分

def rsvp_created(user, rsvp, pdf_file)
  setup_email(user)
  content_type "multipart/mixed"
  @subject << "Your RSVP for #{rsvp.ticket.holiday.title}"

  part :content_type => 'multipart/alternative' do |copy|
    copy.part :content_type => 'text/html' do |html|
      html.body = render( :file => "rsvp_created.text.html.erb", 
                          :body => { :rsvp => rsvp } )
    end
  end

  attachment :content_type => 'application/pdf', 
             :body => File.read(pdf_file), 
             :filename => "#{rsvp.confirmation_number}.pdf"

end
def rsvp_已创建(用户、rsvp、pdf_文件)
设置\u电子邮件(用户)
内容类型“多部分/混合”
@主题“多部分/备选方案”不复制|
copy.part:content_type=>text/html'do | html|
html.body=render(:file=>“rsvp_created.text.html.erb”,
:body=>{:rsvp=>rsvp})
结束
结束
附件:内容类型=>'application/pdf',
:body=>File.read(pdf_文件),
:filename=>“#{rsvp.confirmation_number}.pdf”
结束

谢天谢地,Rails 3.x中的情况似乎并非如此。

我也在尝试做类似的事情,但按照上面道格拉斯的回答,不断收到损坏的pdf附件。通过以二进制模式读取pdf文件,我终于能够解决此问题:

attachment :content_type => 'application/pdf', 
           :body => File.open(pdf_file, 'rb') {|f| f.read} 
           :filename => "#{rsvp.confirmation_number}.pdf" 

我可以用道格拉斯的答案来回答,但也可以用一行字来表达。我使用了一个模板,但这也可以通过用“rsvp”代替render_message方法来实现

part :content_type => "text/html", 
     :body => render_message("template_name", { :symbol => value } )

我喜欢Rails 3。但我正在做一个在2.3.11中开发的项目。所以我不得不放弃Rails的3件事。谢谢你的回复。