How can I make it so ActionMailer always shows attachments at the bottom of the message: HTML, TXT, Attachments....
Problem is the attachment here is a text file:
----==_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>
Thanks
I had the same problem, and in my case the solution was to swap the attachment and mail lines. First attach, then call mail.
Rails 3
WRONG
def pdf_email(email, subject, pdfname, pdfpath)
mail(:to => email, :subject => subject)
attachments[pdfname] = File.read(pdfpath)
end
GOOD
def pdf_email(email, subject, pdfname, pdfpath)
attachments[pdfname] = File.read(pdfpath)
mail(:to => email, :subject => subject)
end
I know there is already an accepted answer, but switching the order of attachments[]
and mail()
didn't solve it for me. What is different about my situation is that I was trying to attach a text file attachment (.txt)
What works for me is setting the content_type
and parts_order
defaults for the mailer.
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
If you are trying to send an email in Rails 3 with a plain text file (.txt), trying adding :content_type
and parts_order
to your defaults so that the text file does not appear above the message in your email.
this is rail 2.3 code (might be slightly different in rails3)
just move you text part before attachment
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
来源:https://stackoverflow.com/questions/5453052/rails-actionmailer-sometimes-shows-attachments-before-the-email-content