Ruby on rails Rails ActionMailer附件保留空白正文

Ruby on rails Rails ActionMailer附件保留空白正文,ruby-on-rails,ruby-on-rails-3,html-email,actionmailer,email-attachments,Ruby On Rails,Ruby On Rails 3,Html Email,Actionmailer,Email Attachments,我正在尝试用ActionMailer生成的电子邮件发送内联附件(图像)。然而,每次我添加一个附件,我总是得到一个空白的电子邮件正文。我试图生成一封简单的测试电子邮件来消除其他变量,但我一直遇到同样的问题 我使用的是Rails 3.2.13,下面是我的代码和失败的规范: app/mailers/contact_mailer.rb class ContactMailer < ActionMailer::Base default from: "support@example.com"

我正在尝试用ActionMailer生成的电子邮件发送内联附件(图像)。然而,每次我添加一个附件,我总是得到一个空白的电子邮件正文。我试图生成一封简单的测试电子邮件来消除其他变量,但我一直遇到同样的问题

我使用的是Rails 3.2.13,下面是我的代码和失败的规范:

app/mailers/contact_mailer.rb

class ContactMailer < ActionMailer::Base
  default from: "support@example.com"

  def test
    attachments.inline['logo.png'] = File.read Rails.root.join('app/assets/images/emails/logo.png').to_s
    mail(to: 'my_email@gmail.com', subject: 'Testing, Testing')
  end

  def test_no_attachment
    mail(to: 'my_email@gmail.com', subject: 'Testing, Testing')
  end
<p>This is a test.</p>
<p>This is only a test.</p>
<%= image_tag attachments['mhbo_logo.png'].url %>
<p>This is a test.</p>
<p>This is only a test.</p>
require 'spec_helper'

describe ContactMailer do
  describe 'test' do
    it 'should send' do
      ContactMailer.test.deliver
      ActionMailer::Base.deliveries.count.should eq 1
      ActionMailer::Base.deliveries.last.body.should match /This is a test./
    end
  end

  describe 'test_no_attachment' do
    it 'should send' do
      ContactMailer.test_no_attachment.deliver
      ActionMailer::Base.deliveries.count.should eq 1
      ActionMailer::Base.deliveries.last.body.should match /This is a test./
    end
  end
end
module TestingMacros
  def last_email_html_body
    ActionMailer::Base.deliveries.last.body.parts.detect{|p| p.content_type.match(/text\/html/)}.body
  end
end
…
last_email_html_body.should match /This is a test./
…
第二次测试通过,但第一次测试失败,原因如下:

 Failure/Error: ActionMailer::Base.deliveries.last.body.should match /This is a test./
   expected  to match /This is a test./
 # ./spec/mailers/contact_mailer_spec.rb:8:in `block (3 levels) in <top (required)>'
失败/错误:ActionMailer::Base.deliveries.last.body.should match/这是一个测试/
预期匹配/这是一个测试/
#./spec/mailers/contact_-mailer_-spec.rb:8:in'block(3层)in'
因此,这封电子邮件的正文是空白的

我的代码可能有什么问题?还是我测试的方式有问题?我测试过许多其他具有相同语法的电子邮件,但没有一封带有附件。我还感到困惑,因为我认为添加附件会发送多部分电子邮件,因此
ActionMailer::Base.deliveries.count
将大于1。我弄错了吗


我在这里有点迷路了,所以非常感谢所有的帮助。

结果表明,我没有正确地测试电子邮件内容。多部分电子邮件(如带有附件的电子邮件)作为单个电子邮件发送,但包含多个部分,访问方式如下:

ActionMailer::Base.deliveries.last.body.parts
因此,为了找到与HTML主体对应的部分,我编写了以下测试:

ActionMailer::Base.deliveries.last.body.parts.detect{|p| p.content_type.match(/text\/html/)}.body.should match /This is a test./
我将此代码重构为宏,以使测试更具可读性:

spec/macros/testing_macros.rb

class ContactMailer < ActionMailer::Base
  default from: "support@example.com"

  def test
    attachments.inline['logo.png'] = File.read Rails.root.join('app/assets/images/emails/logo.png').to_s
    mail(to: 'my_email@gmail.com', subject: 'Testing, Testing')
  end

  def test_no_attachment
    mail(to: 'my_email@gmail.com', subject: 'Testing, Testing')
  end
<p>This is a test.</p>
<p>This is only a test.</p>
<%= image_tag attachments['mhbo_logo.png'].url %>
<p>This is a test.</p>
<p>This is only a test.</p>
require 'spec_helper'

describe ContactMailer do
  describe 'test' do
    it 'should send' do
      ContactMailer.test.deliver
      ActionMailer::Base.deliveries.count.should eq 1
      ActionMailer::Base.deliveries.last.body.should match /This is a test./
    end
  end

  describe 'test_no_attachment' do
    it 'should send' do
      ContactMailer.test_no_attachment.deliver
      ActionMailer::Base.deliveries.count.should eq 1
      ActionMailer::Base.deliveries.last.body.should match /This is a test./
    end
  end
end
module TestingMacros
  def last_email_html_body
    ActionMailer::Base.deliveries.last.body.parts.detect{|p| p.content_type.match(/text\/html/)}.body
  end
end
…
last_email_html_body.should match /This is a test./
…
spec/mailer/contact\u mailer\u spec.rb

class ContactMailer < ActionMailer::Base
  default from: "support@example.com"

  def test
    attachments.inline['logo.png'] = File.read Rails.root.join('app/assets/images/emails/logo.png').to_s
    mail(to: 'my_email@gmail.com', subject: 'Testing, Testing')
  end

  def test_no_attachment
    mail(to: 'my_email@gmail.com', subject: 'Testing, Testing')
  end
<p>This is a test.</p>
<p>This is only a test.</p>
<%= image_tag attachments['mhbo_logo.png'].url %>
<p>This is a test.</p>
<p>This is only a test.</p>
require 'spec_helper'

describe ContactMailer do
  describe 'test' do
    it 'should send' do
      ContactMailer.test.deliver
      ActionMailer::Base.deliveries.count.should eq 1
      ActionMailer::Base.deliveries.last.body.should match /This is a test./
    end
  end

  describe 'test_no_attachment' do
    it 'should send' do
      ContactMailer.test_no_attachment.deliver
      ActionMailer::Base.deliveries.count.should eq 1
      ActionMailer::Base.deliveries.last.body.should match /This is a test./
    end
  end
end
module TestingMacros
  def last_email_html_body
    ActionMailer::Base.deliveries.last.body.parts.detect{|p| p.content_type.match(/text\/html/)}.body
  end
end
…
last_email_html_body.should match /This is a test./
…

因为这让我明白,如果有附件,我需要处理email.body.parts,而不仅仅是将email.body直接呈现为HTML blob。谢谢你带我去那里。