title: Rails ActionMailer attachment leave blank body
body: <p>I'm trying to send an inline attachment (an image) with my ActionMailer-generated email. However, every time I add an attachment, I keep getting a blank email body. I tried generating a bare-bones test email to eliminate other variables in play, but I keep getting the same issue.</p>
  
  <p>I'm using Rails 3.2.13, and the following is my code and failing specs:</p>
  
  <p><strong>app/mailers/contact_mailer.rb</strong></p>
  
  <pre><code>class ContactMailer &lt; 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
  </code></pre>
  
  <p><strong>app/views/contact_mailer/test.html.erb</strong></p>
  
  <pre><code>&lt;p&gt;This is a test.&lt;/p&gt;
  &lt;p&gt;This is only a test.&lt;/p&gt;
  &lt;%= image_tag attachments['mhbo_logo.png'].url %&gt;
  </code></pre>
  
  <p><strong>app/views/contact_mailer/test_no_attachment.html.erb</strong></p>
  
  <pre><code>&lt;p&gt;This is a test.&lt;/p&gt;
  &lt;p&gt;This is only a test.&lt;/p&gt;
  </code></pre>
  
  <p><strong>spec/mailers/contact_mailer_spec.rb</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p>The second test passes but the first test fails with the following:</p>
  
  <pre><code> 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 &lt;top (required)&gt;'
  </code></pre>
  
  <p>Thus, the body of this email is blank.</p>
  
  <p>What could be wrong with my code? or is something wrong with the way I'm testing this? I've tested many other emails with the same syntax, though granted none with attachments. I'm also confused because I thought adding attachments would send multi-part emails, so that <code>ActionMailer::Base.deliveries.count</code> would be greater than 1. Am I mistaken in that?</p>
  
  <p>I'm a bit lost here, so any and all help would be much appreciated.</p>