Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set subject from ActionMailer template in Rails 3?

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/)

like image 852
PBJ Avatar asked Sep 29 '11 06:09

PBJ


People also ask

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.

What is action mailer?

Action Mailer allows you to send emails using mailer classes and views. Mailers work very similarly to controllers.


2 Answers

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
like image 186
corroded Avatar answered Sep 22 '22 20:09

corroded


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.

like image 21
chrisgooley Avatar answered Sep 19 '22 20:09

chrisgooley