开发者

How Do I Prevent Email Attachments from Rendering Inline using ActionMailer

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开发者_StackOverflow 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?


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

Notice the content-disposition line.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜