2011-06-09 30 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 }) 
相關問題