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.
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.
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.
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
This article answer to your question. Just specify :layout
option in render
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With