Rails mailer with various layouts

I use one layout for all my emails in my Notifier model (20+ emails) ... however sometimes I just want to send a text email without a layout or html. It seems I can’t figure out how to do this? If I try to send a text email, I still get the layout and all the HTML in the email.

I am using Rails 2.3.8.

I read about this monkey patch here ... but did it seem to indicate that the newer version of the rails was over? And I really don't want a monkey patch if I can avoid it.

Rails - setting up multiple layouts for multi-page email with email templates

layout "email" # use email.text.(html|plain).erb as the layout def welcome_email(property) subject 'New Signup' recipients property.email from 'welcome@test.com' body :property => property content_type "text/html" end def send_inquiry(inquire) subject "#{inquire.the_subject}" recipients inquire.ob.email from "Test on behalf of #{inquire.name} <#{inquire.email}>" body :inquire => inquire content_type "text/plain" end 

I also have 2 files.

 email.text.html.erb email.text.plain.erb 

It always uses text.html.erb ... even if the content_type is "text / plain"

+7
email ruby-on-rails layout multipart actionmailer
source share
3 answers

edit: It turned out that the layouts follow a different naming scheme for email templates. Just rename them as follows:

 layout.text.html.erb => layout.html.erb layout.text.plain.erb => layout.text.erb 

I also made a mistake when manually defining parts if you use this:

 part :content_type => 'text/plain', :body => render_message('my_template') 

Rails then cannot determine the content_type for your part and assumes that it is HTML.

After I changed these two things, it worked for me!

The original answer follows.


I have struggled with this issue many times in the past, usually ending with some kind of dry, quick and dirty solution. I always thought that I was the only one who has this problem, because Google has nothing useful in this matter.

This time I decided to delve into Rails to understand this, but so far without much success, but perhaps my results will help someone understand this.

I found that in ActionMailer :: Base, the #render_message method was given the definition of the correct content_type and assigned it @current_template_content_type. #default_template_format, it either returns the correct mime type for the layout, or if @current_template_content_type is not set, it will be by default: html.

This is what ActionMailer :: Base # render_message looks like in my application (2.3.5)

  def render_message(method_name, body) if method_name.respond_to?(:content_type) @current_template_content_type = method_name.content_type end render :file => method_name, :body => body ensure @current_template_content_type = nil end 

The problem is that the method name is a string (the name of the local view, in my case "new_password.text.html"), and the strings, of course, do not respond to_content_type, and the value of @current_template_content_type will always remain null, and therefore #default_template_format will always will be the default: html.

I do not know what is closer to the real solution. The internal elements of ActionMailer are too opaque to me.

+9
source share

OK, not sure if this works, but it looks like the content_type is text / plain by default, so you only need to set the content type if you want something other than text / plain.

Try the following:

 def send_inquiry(inquire) subject "#{inquire.the_subject}" recipients inquire.ob.email from "Test on behalf of #{inquire.name} <#{inquire.email}>" body :inquire => inquire end 

I still think you should consider this:

 layout "email", :except => [:send_inquiry] 

I would take advantage of the above because the text email does not seem to have a β€œlayout”, but only the actual content that you want to send.

+5
source share

I found this, I think it might be useful.

http://blog.blazingcloud.net/2009/11/17/simple-email-form-with-actionmailer/

It uses renaming of presentation templates for different types of content.

0
source share

All Articles