Kat
Kat

Reputation: 31

RoR ActionMailer sending inline attachments instead of regular ones

I try to send NOT INLINE attachment by mail:

m = ActionMailer::Base.mail(:to => "[email protected]", :from => "[email protected]", :subject=>"test")
m.attachments["test.csv"] = File.read("#{Rails.root}/lib/tasks/test.csv")
m.deliver

When recipient is getting mail, attachment is showing INLINE (mail's body contains text of attachment).

Rails 3.2.19 Ruby 2.1.2p95

Give me advice, please, how to fix it and send attachment like file attached to mail ;)

Upvotes: 1

Views: 1598

Answers (2)

Kat
Kat

Reputation: 31

This work for me:

class ReportMailer < ActionMailer::Base
    def send_report
        attachments["test.csv"] = {mime_type: 'text/csv', content: File.read(Rails.root.join('lib', 'tasks', 'test.csv'))}
        mail(:to => "", :from => "", :subject=>"") do |format|
                format.text {render :text => ""}
        end
    end
end

ReportMailer.send_report.deliver

Upvotes: 2

Ivan Shamatov
Ivan Shamatov

Reputation: 1416

Try to specify mime type and content disposition:

m.attachments["test.csv"] = {mime_type: 'text/csv',                            
                             content: File.read("#{Rails.root}/lib/tasks/test.csv"),
                             content_disposition: 'attachment'}

Upvotes: 0

Related Questions