Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change mailer layout in method

How do I change the overall layout of an email in the method within a mailer?

Trying to essentially do this:

class AccountMailer < ActionMailer::Base
  layout 'mailer'

  def reset_password(user)
    layout 'simple_mailer'
  end
end

But that throws an error.

Basically I have a layout in /app/views/layouts/simple_mailer.html.erb that I'd like to use, but only in that one method.

I'm running Rails 4.2.

like image 627
Shpigford Avatar asked Mar 04 '15 15:03

Shpigford


People also ask

What is action mailer?

1 Introduction. Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.

How do I send an email to ROR?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.


2 Answers

The mail method can take a block that passes in a format object.

class MyMailer < ApplicationMailer
  layout 'mailer'
  def password_reset(user)
   # uses the "mailer" layout
  end

  def special
    mail(to: "whomever") do |format|
      format.html { render(layout: false) } # no layout is used
      format.text # use the special.text.erb like normal
    end
  end
end
like image 129
jeremywoertink Avatar answered Oct 25 '22 04:10

jeremywoertink


This article answer to your question. Just specify :layout option in render method.

like image 31
Maxim Avatar answered Oct 25 '22 02:10

Maxim