views:

195

answers:

2

I am using the following code to send an email with a pdf attachment:

class StudyMailer < ActionMailer::Base
  def notify_office(study, sent_at = Time.now)
    subject    "Email Subject Goes Here"
    recipients '[email protected]'
    from       "#{study.sender.full_name} <#{study.sender.email}>"
    sent_on    sent_at
    body       :study => study
    for document in study.documents   
      attachment :content_type => "application/pdf", :body => File.read(document.document.path) #absolute path to .pdf document
    end
  end
end

When the email is sent, the attachment seems to render inline as binary code rather than as a .pdf attachment.

How do I render the .pdf as a typical attachment, rather than inline?

A: 
attachment :content_type => "application/pdf", 
    :content_disposition => "attachment", 
    :filename => File.basename(fattach), 
    :body => File.new(fattach,'rb').read()

Notice the content-disposition line.

CodeJoust
adding the attachement in a block does not seem to affect how the attachment is rendered
rswolff
turns out this problem had more to do with the name of the email view template than anything else, but I'm marking this as the answer because the content_disposition piece was also needed.
rswolff
A: 

I believe you have to indicate the multipart nature of the email, so add this line under the from line:

content_type    "multipart/alternative"
khelll
adding the content_type does seem not affect how the attachment is rendered.
rswolff