Lee McAlilly
Lee McAlilly

Reputation: 9316

How do you put dynamic content in an action mailer subject line?

I'm trying to put the name of my app in the subject line of emails that action mailer sends. I have no problem doing this with the default from email address, but when I try to add it to the subject it sends it through as plain text <%= app_name %>.

Here's my mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base  
  add_template_helper(ApplicationHelper)
  extend ApplicationHelper

  default from: "#{app_name} <#{system_email}>"

  def reset_password_email(user)
    @user = user
    @url  = "#{root_url}/password_resets/#{user.reset_password_token}/edit"
    mail(:to => user.email,
         :subject => "Reset Your Password | #{app_name}")
  end
end

And here's what I have in my application helper:

  def app_name
    'Tip Share'
  end

  def app_domain
    'tipshare.herokuapp.com'
  end

  def system_email
    '[email protected]'
  end

Neither #{app_name} or <%= app_name %> works. How do I do this?

Upvotes: 1

Views: 845

Answers (2)

iosctr
iosctr

Reputation: 471

By extending UserMailer with ApplicationHelper, those methods are available as class methods - which is why the reference in default from: "#{app_name} <#{system_email}>" works. Within the reset_password_email method, app_name would be an undefined local variable. So you either need to additionally include ApplicationHelper, or call it with self.class.app_name.

Upvotes: 0

iouri
iouri

Reputation: 2929

Have you tried adding it like this:

include ApplicationHelper

Upvotes: 1

Related Questions