如何制作,以便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>
由于
答案 0 :(得分:5)
我知道已经有一个已接受的答案,但是切换attachments[]
和mail()
的顺序并没有为我解决。我的情况有什么不同,我试图附加一个文本文件附件(.txt)
对我有用的是为邮件设置content_type
和parts_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_type
和parts_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