How do I set the subject from a Rails 3 ActionMailer template?
I'd like to keep the subject and body together.
In Rails 2 you can do the following:
<% controller.subject = 'Change of subject' %>
(from http://www.quirkey.com/blog/2008/08/29/actionmailer-changing-the-subject-in-the-template/)
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.
Action Mailer allows you to send emails using mailer classes and views. Mailers work very similarly to controllers.
http://edgeguides.rubyonrails.org/action_mailer_basics.html
it says here that:
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
def welcome_email(user)
@user = user
@url = "http://example.com/login"
mail(:to => user.email, :subject => "Welcome to My Awesome Site")
end
end
If you want to access it from the view:
http://apidock.com/rails/ActionMailer/Base
If you need to access the subject, from or the recipients in the view, you can do that through message object:
You got a new note from <%= message.from %>!
<%= truncate(@note.body, 25) %>
So you can do:
message.subject
To set the subject line of an email from the email view itself you can simply put the following at the beginning of the view file:
<% message.subject = 'This is my subject line' %>
This works for rails 3 and 4.
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