Rails - ActionMailer有时会在电子邮件内容之前显示附件?

时间:2011-03-27 22:56:13

标签: ruby-on-rails ruby-on-rails-3 actionmailer

如何制作,以便ActionMailer始终在邮件底部显示附件: HTML,TXT,附件......

问题是这里的附件是一个文本文件:

----==_mimepart_4d8f976d6359a_4f0d15a519e35138763f4
Date: Sun, 27 Mar 2011 13:00:45 -0700
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=x00_129999.olk14message
Content-ID: <4d8f976d49c72_4f0d15a519e351387611f@railgun64.53331.mail>

由于

3 个答案:

答案 0 :(得分:5)

我知道已经有一个已接受的答案,但是切换attachments[]mail()的顺序并没有为我解决。我的情况有什么不同,我试图附加一个文本文件附件(.txt)

对我有用的是为邮件设置content_typeparts_order默认值。

MyMailer < ActionMailer::Base

    default :from => "Awesome App <support@example.com>",
            :content_type => 'multipart/alternative',
            :parts_order => [ "text/html", "text/enriched", "text/plain", "application/pdf" ]

    def pdf_email(email, subject, pdfname, pdfpath)
      attachments[pdfname] = File.read(pdfpath)
      mail(:to => email, :subject => subject)
    end

    def txt_email(email, subject, filename, filebody)
      attachments[filename] = filebody
      mail(:to => email, :subject => subject)
    end
end

如果您尝试使用纯文本文件(.txt)在Rails 3中发送电子邮件,请尝试将:content_typeparts_order添加到默认值,以便文本文件不会显示在电子邮件中的消息。

答案 1 :(得分:4)

我有同样的问题,在我的情况下解决方案是交换附件和邮件行。首先附上,然后打电话给邮件。

Rails 3

<强> WRONG

def pdf_email(email, subject, pdfname, pdfpath)
  mail(:to => email, :subject => subject)
  attachments[pdfname] = File.read(pdfpath)
end

不可

def pdf_email(email, subject, pdfname, pdfpath)
  attachments[pdfname] = File.read(pdfpath)
  mail(:to => email, :subject => subject)
end

答案 2 :(得分:0)

这是rails 2.3代码(在rails3中可能略有不同)

只需在附件之前移动文字部分

recipients  to@domain.com
from      me@domain.com
subject   "some subject"
content_type  "multipart/mixed"

part "text/plain" do |p|
  p.body = render_message 'my_message' #this is template file
end

attachment "application/octet-stream" do |a|
  a.body = File.read("some_file.jpg")
  a.filename = 'name.jpg'
end