title: Rails ActionMailer attachments show up as garbled text
body: <p>I'm trying to send pdf attachments in one of my rails applications, but the attachment comes through as text instead of as a proper pdf.</p>
  
  <p>My mailer looks like this:</p>
  
  <pre><code>class Notifications &lt; ActionMailer::Base
    def contact(email_params, sent_at = Time.now)
      subject "" &lt;&lt; email_params[:subject]
      recipients "" &lt;&lt; email_params[:client]
      from "#{email_params[:name]} &lt;#{email_params[:address]}&gt;"
      attachments['invoice.pdf'] = File.read("#{Rails.root.to_s}/public#{email_params[:attach]}") unless email_params[:attach].blank?
      sent_on sent_at
      body :message =&gt; email_params[:body], :sender_name =&gt; email_params[:name]
    end
  end
  </code></pre>
  
  <p>And what I get in my mail inbox:</p>
  
  <pre><code>--
  Date: Thu, 23 Feb 2012 13:50:58 -0800
  Mime-Version: 1.0
  Content-Type: text/plain;
  charset=UTF-8
  Content-Transfer-Encoding: 7bit
  Content-ID: &lt;[mailserver]&gt;
  
  Message Body
  
  --
  Date: Thu, 23 Feb 2012 13:50:58 -0800
  Mime-Version: 1.0
  Content-Type: application/pdf;
  charset=UTF-8;
  filename=invoice.pdf
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment;
  filename=invoice.pdf
  Content-ID: &lt;[mailserver]&gt;
  </code></pre>
  
  <p>Followed by a very long string of ascii characters</p>
  
  <p>Without any attachment, that header information is not sent, and I just get the "Message Body" part in my inbox. I suspect the File.read() part of the attachment line in my mailer.rb is somehow the culprit, but it is pointing to a valid pdf file on my server which I can access through my browser.  Logs only say the mail was sent with the correct parameters, but it's being sent as what looks like decompiled text or something like if you opened a pdf in a plain text editor.</p>
  
  <p>The mailer view, contact.html.erb, is simply: <code>&lt;%= @message %&gt;</code> If that helps.  </p>
  
  <p>Any idea what I might be doing wrong?  </p>
  
  <p>Thanks.</p>
  
  <p>Update:</p>
  
  <p>Apparently, I needed both contact.erb, as well as contact.html.erb in my views directory.  Just copying the file, and renaming it worked.  However, now the message body is blank.  Only the attachment shows up.</p>