Rails mailer without view
Is it possible to have a Rails mailer without any view ?
When dealing with text-only mails, it would be cool to be able to put the body right in the mailer itself and not have to use a view for the action with just one line of text (or one I18n key).
In a way, I'm looking for something like ActionController's "render :text =>" but for Act开发者_如何学运维ionMailer.
Much simpler, just use the body option :
def welcome(user)
mail to: user.email,
from: "\"John\" <admin@domain.ch>",
subject: 'Welcome in my site',
body: 'Welcome, ...'
end
And if you plan to use html, don't forget to specify that with the content_type option which is by default text/plain.
content_type: "text/html"
So with body option rails skips the template rendering step.
I found the way by experimentation :
mail(:to => email, :subject => 'we found the answer') do |format|
format.text do
render :text => '42 owns the World'
end
end
This is also said in the Rails Guide : http://guides.rubyonrails.org/action_mailer_basics.html at section 2.4
精彩评论