<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>class FormMailer &lt; ActionMailer::Base

  def contact_us(params)
    from params[:email]
    recipients params[:to]
    subject params[:subject]
    body :params =&gt; params
  end

  def help_request(params)
    from params[:email]
    recipients params[:to]
    subject params[:subject]
    body :params =&gt; params
  end

  def service(params)
    from params[:email]
    recipients params[:to]
    subject params[:subject]
    body :params =&gt; params
  end

  def customer_care(params)
    from params[:email]
    recipients params[:to]
    subject params[:subject]
    body :params =&gt; params
  end

  def tellafriend(params)
    from params[:from_email]
    recipients params[:friend_email]
    headers 'Disposition-Notification-To' =&gt; params[:from_email] if params[:notify] == 'yes'
    subject params[:subject]
    body :params =&gt; params
  end

  # snip... 20 more
end
</code>
  <comment>Here is a FormMailer that I have.  As you know, ActionMailer is a Model not a controller.  But I have yet to figure out how to reduce this to one method instead of 20...</comment>
  <created-at type="datetime">2008-03-01T19:19:54+00:00</created-at>
  <id type="integer">246</id>
  <language>Ruby</language>
  <permalink>action-mailer-it-has-to-be-easier</permalink>
  <refactors-count type="integer">4</refactors-count>
  <title>Action Mailer - It has to be easier?</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2009-11-13T18:38:04+00:00</updated-at>
  <user-id type="integer">49</user-id>
  <refactors type="array">
    <refactor>
      <code>class FormMailer &lt; ActionMailer::Base
  def send_email(params)
    from params[:email]
    recipients params[:to]
    subject params[:subject]
    body :params =&gt; params
  end

  def customer_care(params) send_email(params); end
end</code>
      <code-id type="integer">246</code-id>
      <comment>You can probably make this even more dry with alias_method, but I'm not sure how that would interact with the magic ActionMailer methods.</comment>
      <created-at type="datetime">2008-03-02T02:08:12+00:00</created-at>
      <id type="integer">3590</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Action Mailer - It has to be easier?</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Emmett</user-name>
      <user-website>www.justin.tv</user-website>
    </refactor>
    <refactor>
      <code>class FormMailer &lt; ActionMailer::Base

  %w{contact_us help_request service customer_care}.each do |method_name|

    define_method(method_name) do |params|
      from params[:email]
      recipients params[:to]
      subject params[:subject]
      body :params =&gt; params
    end

  end

end</code>
      <code-id type="integer">246</code-id>
      <comment>You could try some meta-programming.  This way your list of method names is nice and centralized.</comment>
      <created-at type="datetime">2008-03-02T04:00:31+00:00</created-at>
      <id type="integer">3594</id>
      <language>Ruby</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Action Mailer - It has to be easier?</title>
      <user-id type="integer">178</user-id>
      <user-name>Barry Hess</user-name>
      <user-website>http://blog.bjhess.com</user-website>
    </refactor>
    <refactor>
      <code>def method_missing(m, *args)      
  unless m.to_s[0, 8] == 'deliver_'
    from params[:email]
    recipients params[:to]
    subject params[:subject]
    body :params =&gt; params
  end
end</code>
      <code-id type="integer">246</code-id>
      <comment>Haven't had much sleep and therefore my mind might have produced some crappy idea, but maybe might be able to elaborate a little bit more on it. What I would suggest is using the method_missing functionality in order to create the method. This might interfere with the parents implementation for creating the "deliver_" prefixed methods. But I am not that into metaprogramming. Moreover, one might wanna add some check if the rendering of the template works.</comment>
      <created-at type="datetime">2008-03-02T20:36:29+00:00</created-at>
      <id type="integer">3619</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Action Mailer - It has to be easier?</title>
      <user-id type="integer">212</user-id>
      <user-name>namxam</user-name>
      <user-website>http://max.jungeelite.de/</user-website>
    </refactor>
    <refactor>
      <code>class FormMailer &lt; ActionMailer::Base
  before_deliver :prep_email

  def contact_us(params)
  end

  def help_request(params)
  end

  def service(params)
  end

  def customer_care(params)
  end

  def tellafriend(params)
    headers 'Disposition-Notification-To' =&gt; params[:from_email] if params[:notify] == 'yes'
  end

  protected
  def prep_email
    from params[:from_email]
    recipients params[:friend_email]
    subject params[:subject]
    body :params =&gt; params
  end
end
</code>
      <code-id type="integer">246</code-id>
      <comment>This seems like a job for filters.  Unfortunately, ActionMailer isn't ActionController, and doesn't have them.  But if you're feeling lucky, there is a plugin.  

http://www.pathf.com/blogs/2009/01/actionmailer-callbacks-in-the-spirit-of-actioncontroller-filters/

I guess you'd end up with something like:</comment>
      <created-at type="datetime">2009-11-13T18:37:44+00:00</created-at>
      <id type="integer">359685</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Action Mailer - It has to be easier?</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Bryce</user-name>
      <user-website>http://twitter.com/darth_schmoo</user-website>
    </refactor>
  </refactors>
</code>
