如何使用ActionMailer防止使用内联呈现电子邮件附件

时间:2009-12-15 05:14:33

标签: ruby-on-rails actionmailer email-attachments

我使用以下代码发送包含pdf附件的电子邮件:

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

发送电子邮件时,附件似乎呈现内联为二进制代码而不是.pdf附件。

如何将.pdf渲染为典型附件,而不是内联?

3 个答案:

答案 0 :(得分:1)

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

注意内容处置行。

答案 1 :(得分:0)

我认为您必须指明电子邮件的多部分性质,因此请在from行下添加此行:

content_type    "multipart/alternative"

答案 2 :(得分:0)

您的电子邮件是否有模板?如果电子邮件没有模板,即使其他所有内容都已正确设置,附件也会显示为内联。创建附件电子邮件模板。

  

视图/通知/ attachment.html.erb

<p>   Please see attachment    </p>

然后在通知程序中,指定使用此模板。

  

notifier.rb

def my_email_method
    ...
    mail(:template_name => 'attachment', :from => from_address, ...)
end
相关问题