Rails - ActionMailer sometimes shows attachments before the email content?

后端 未结 3 1682
温柔的废话
温柔的废话 2021-01-13 01:54

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:

3条回答
  •  别那么骄傲
    2021-01-13 02:21

    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 ",
                :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.

提交回复
热议问题