aurels
aurels

Reputation: 801

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 ActionMailer.

Upvotes: 19

Views: 4693

Answers (2)

Noémien Kocher
Noémien Kocher

Reputation: 1354

Much simpler, just use the body option :

def welcome(user)
  mail to:       user.email,
       from:     "\"John\" <[email protected]>",
       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.

Upvotes: 24

aurels
aurels

Reputation: 801

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

Upvotes: 19

Related Questions