Cameron Booth
Cameron Booth

Reputation: 6992

Rails - setting multiple layouts for a multipart email with mailer templates

So Rails 2.2 added mailer layouts, which is great, except that I can't figure out how to make them work when I'm sending a multipart email..it's wrapping my mail content with the same layout for both the text/plain version and the text/html version. What I want is to wrap my layout around either only the text/html version, or to be able to have a separate layout for each.

Anybody encountered this? I haven't seen any mention of it elsewhere,

Cameron

Upvotes: 2

Views: 3271

Answers (1)

paulthenerd
paulthenerd

Reputation: 9507

For future reference the solution in the blog post above amended in a second blog post is given below all credit to the above mentioned blog post. Solution blog post

Add this code to your environment.rb file to stop the mailer from applying layouts to plain text emails. It also has a check that will stop it from conflicting with the exception notification plugin.

# Do not use the mailer layout template for plain text emails
module ActionMailer
  class Base
    private    
    def candidate_for_layout?(options)
       (!options[:file] || !options[:file].respond_to?(:content_type) ||
          options[:file].content_type != 'text/plain') &&
          [email protected](:_exempt_from_layout?, default_template_name)
    end
  end
end

Upvotes: 3

Related Questions