<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Flvorful Bloggage: Deep in Rails: ActionMailer#deliver Part VI</title>
    <link>http://blog.flvorful.com/articles/2009/12/20/deep-in-rails-actionmailer-deliver-part-vi</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Deep in Rails: ActionMailer#deliver Part VI</title>
      <description>&lt;p&gt;So now we know how Rails sends emails.  What can we do with that knowledge?&lt;/p&gt;

&lt;p&gt;All kinds of stuff.&lt;/p&gt;

&lt;p&gt;One of my clients needed the ability to send messages through 3 different &lt;span class="caps"&gt;SMTP&lt;/span&gt; servers.  They wanted it to be easy to use for their developers and allow them to change out &lt;span class="caps"&gt;SMTP&lt;/span&gt; servers for each mailer method.  Now that we know how AM does it&#8217;s thing, we can use that knowledge to create a quick patch and solve the above problem.&lt;/p&gt;

&lt;p&gt;We&#8217;ll create a new file in out Rails app inside lib/ and call it &lt;code&gt;action_mailer_hacks.rb&lt;/code&gt;.  Inside of the file add the following (dont worry, we&#8217;ll go over each line)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module ActionMailerHacks

    module InstanceMethods
        def deliver_with_switchable_smtp!(mail = @mail)
            unless logger.nil?
                logger.info  "Switching SMTP server to: #{new_smtp.inspect}" 
            end
            ActionMailer::Base.smtp_settings = new_smtp unless new_smtp.nil?
            deliver_without_switchable_smtp!(mail = @mail)
        end

    end

    def self.included(receiver)
        receiver.send :include, InstanceMethods
        receiver.class_eval do
            adv_attr_accessor :new_smtp
            alias_method_chain :deliver!, :switchable_smtp
        end
    end
end

ActionMailer::Base.send :include, ActionMailerHacks
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First, in &lt;code&gt;module InstanceMethods&lt;/code&gt;, we create a new deliver method that will switch the &lt;span class="caps"&gt;SMTP&lt;/span&gt; server from AM.  We do some quick logging and then set &lt;code&gt;AM::Base.smtp_settings&lt;/code&gt; to the new smtp server.  After we do that, we call &lt;code&gt;deliver_without_switchable_smtp&lt;/code&gt; which gets created when we do our &lt;em&gt;alias_method_chaining&lt;/em&gt; later down the page.&lt;/p&gt;

&lt;p&gt;When this module is included we include the &lt;code&gt;InstanceMethods&lt;/code&gt; module into the receiver and then run &lt;code&gt;class_eval&lt;/code&gt; so that we can create a new &lt;code&gt;adv_attr_accessor&lt;/code&gt; called &lt;code&gt;new_smtp&lt;/code&gt; and then call &lt;code&gt;alias_method_chain&lt;/code&gt; to create our &lt;code&gt;without&lt;/code&gt; method.  After that we include this whole module into AM::Base and we&#8217;re done with the hack.  Add &lt;code&gt;require "action_mailer_hacks.rb"&lt;/code&gt; to your &lt;code&gt;environment.rb&lt;/code&gt; file and we&#8217;re ready for the mailer.&lt;/p&gt;

&lt;p&gt;Now we move on to MessageMailer and actually use the hack.&lt;/p&gt;

&lt;pre id="scroll_to_here" &gt;&lt;code&gt;class MessageMailer &amp;lt; ActionMailer::Base
  def contact_form(msg_from, message)
    subject    'Im contacting you'
    recipients "someemail@somedomain.com" 
        new_smtp   :address =&amp;gt; 'mail.someplace.com', :port =&amp;gt; 25, :domain =&amp;gt; 'someplace.com', :authentication =&amp;gt; :login, :user_name =&amp;gt; 'xxxxdddd', :password =&amp;gt; 'secret'
    from       msg_from
    sent_on    Time.now
    body       :message =&amp;gt; message
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All we do is call &lt;code&gt;new_smtp&lt;/code&gt; with our new server address and then this method gets delivered through that &lt;span class="caps"&gt;SMTP&lt;/span&gt; server.  That&#8217;s it.  Simple and clean, exactly how I like it.&lt;/p&gt;

&lt;p&gt;Well, that was fun.  Next time, we&#8217;ll look through something a little shorter, maybe migrations.  Until then, have fun and keep codin&#8217;&lt;/p&gt;</description>
      <pubDate>Sun, 20 Dec 2009 09:41:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f0e64747-3896-444c-b2ae-d02320362aa1</guid>
      <author>jake</author>
      <link>http://blog.flvorful.com/articles/2009/12/20/deep-in-rails-actionmailer-deliver-part-vi</link>
      <category>Rails</category>
      <category>rails</category>
      <category>action_mailer</category>
      <category>deep</category>
      <category>DIR</category>
      <category>part4</category>
    </item>
  </channel>
</rss>
