Question

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 'user@domain.come'
    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?

Was it helpful?

Solution

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

Notice the content-disposition line.

OTHER TIPS

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

content_type    "multipart/alternative"

Does your email have a template? If the email does not have a template, the attachment shows up inline even if everything else is set up correctly. Create an attachment email template.

views/notifier/attachment.html.erb

<p>   Please see attachment    </p>

Then in Notifier, specify to use this template.

notifier.rb

def my_email_method
    ...
    mail(:template_name => 'attachment', :from => from_address, ...)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top