当电子邮件带有附件时,Rails 2.3.11 ActionMailer 不会呈现 html

发布于 2024-11-15 06:26:27 字数 1111 浏览 2 评论 0原文

我正在使用 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

rsvp_cancelled 工作正常并正确发送电子邮件。但带有附件的 rsvp_created 电子邮件无法正常工作。它发送带有附件的电子邮件,但不呈现任何文本。有人以前遇到过这个问题或者知道我该如何解决它吗?

谢谢

I am using Rails 2.3.11. I created a UserMailer with following methods:

     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

The rsvp_cancelled works fine and sends email properly. But the rsvp_created email, which has an attachment, doesn't work properly. It sends the email with the attached file but doesn't render any text. Any one faced this issue before or know how I can resolve it?

Thanks

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

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

发布评论

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

评论(3

つ可否回来 2024-11-22 06:26:27

使用 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 中的情况似乎并非如此。

With Rails 2.x, for some reason or the other you need to define all the parts for the HTML to appear.

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

Thankfully it appears this is not the case in Rails 3.x.

白馒头 2024-11-22 06:26:27

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

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

I was trying to do the same sort of thing, but following Douglas' answer above, kept getting corrupted pdf attachments. I was finally able to resolve the issue by reading the pdf file in binary mode:

attachment :content_type => 'application/pdf', 
           :body => File.open(pdf_file, 'rb') {|f| f.read} 
           :filename => "#{rsvp.confirmation_number}.pdf" 
一绘本一梦想 2024-11-22 06:26:27

我能够让我的答案与道格拉斯的答案一起工作,但也能够让它在一行中工作。我使用的是模板,但这也可以通过用“rsvp”替换 render_message 方法来实现。

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

I was able to get mine working with Douglas' answer but was also able to get it working in one line. I was using a template but this also works by substituting "rsvp" for the render_message method.

part :content_type => "text/html", 
     :body => render_message("template_name", { :symbol => value } )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文