1

我正在使用以下代码发送带有 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 呈现为典型附件,而不是内联?

4

3 回答 3

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

注意内容处置行。

于 2009-12-15T05:20:29.437 回答
0

我相信您必须指出电子邮件的多部分性质,因此在以下行下添加此from行:

content_type    "multipart/alternative"
于 2009-12-15T05:24:31.997 回答
0

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

意见/通知/附件.html.erb

<p>   Please see attachment    </p>

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

通知程序.rb

def my_email_method
    ...
    mail(:template_name => 'attachment', :from => from_address, ...)
end
于 2012-07-24T17:03:06.790 回答