Well I’m writing quite a mail-intensive small application right now, and wanted to use the same template for 2 mail actions at some point. Searching for how to use template different from mailer action name didn’t give results, but looking into the ActionMailer code did (hail the open source!), so I’m placing it here to be a bit more visible )

Well the solution is quite laconic: just set self.template to what you need, just like you would render :action => :smth_different in ActionView. In example below enabled_notification action does it:

class GroupsMailer < ActionMailer::Base
 #...
 def disabled_notification(group, actor, disabled = true)
    verb = disabled ? 'disabled' : 'enabled'
    subject 'Usergroup \'%s\' was %s' % [group.name, verb]

    body :group => group, :actor => actor,
        :group_url => group_url(group.dn), :verb => verb

    common_headers(actor)
  end

  def enabled_notification(group, actor)
    disabled_notification(group, actor, false)
    self.template = :disabled_notification
  end
end

This was with actionmailer-2.0.1.



3 Responses to “reusing templates in ActionMailer”  

  1. 1 andy

    Thank you for this,

    I’ve been searching for a way to alter the ActionMailer template path but it is not documented well anywhere.

    This is the only post that actually worked.

  2. 2 thirstydoh

    Welcome! ) Glad it helped.

  3. Submit a documentation patch :)


Leave a Reply