2011-06-09 55 views
2

我正在使用Rails 2.3.11。我创建了以下方法UserMailer:Rails 2.3.11当电子邮件有附件时,ActionMailer不会呈现HTML

 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 

的rsvp_cancelled工作正常,正常时发送电子邮件。但是有附件的rsvp_created电子邮件无法正常工作。它发送附加文件的电子邮件,但不提供任何文本。任何人在面对这个问题之前或知道我如何解决它?

感谢

回答

2

使用Rails 2.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 

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

+0

我喜欢Rails 3.但是我正在开发一个在2.3.11开发的项目。所以我不得不忘掉Rails 3的东西。谢谢回复。 – user791715 2011-06-10 00:10:02

0

我试图做同样的事情,但遵循道格拉斯的上述答案,不断收到损坏的pdf附件。我终于能够通过阅读PDF文件以二进制方式来解决问题:

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

我能得到矿山与道格拉斯的回答工作,但也能得到它在同一行的工作。我正在使用一个模板,但这也可以通过将“rsvp”替换为render_message方法。

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