title: Altering an email address but not the associated name in a Ruby on Rails Mail interceptor
body: <p><strong>Background:</strong> in a Rails 3.2 app, I have an ActionMailer purchase confirmation email that is manipulated in a "stage" environment so that email destined for addresses associated with payment processor sandbox accounts will actually be sent to the email addresses of the people who manage the sandbox accounts.  This is currently done inside the mailer class: </p>
  
  <pre><code># app/mailers/purchase_mailer.rb
  class PurchaseMailer &lt; ActionMailer::Base
  
    default :from =&gt; "\"#{SiteConfig.name}\" &lt;#{SiteConfig.support_email}&gt;"
  
    def purchase_notification(purchase)
      @purchase = purchase
  
      mail :to =&gt; "\"#{purchase.customer_name}\" &lt;#{address_filter(purchase.customer_email)}&gt;",
           :subject =&gt; "[#{SiteConfig.name}] Purchase Confirmation"
    end
  
    private
  
    def address_filter(email_address)
      # Check for and remove sandbox identifiers
      if Rails.env.stage?
        email_address.sub(/_\d+_p(er|re)@/, '@')
      else
        email_address
      end
    end
  
  end
  </code></pre>
  
  <p>But, hey, that looks like a great use case for an <a href="http://guides.rubyonrails.org/action_mailer_basics.html#intercepting-emails" rel="nofollow">interceptor</a>, no?  So I pulled out the address_filter method above and added this to the Rails app.</p>
  
  <pre><code># config/initializers/mail.rb
  Mail.register_interceptor(StageMailInterceptor) if Rails.env.stage?
  
  # lib/stage_mail_interceptor.rb
  class StageMailInterceptor
  
    def self.delivering_email(message)
      receivers = []
      message.to.each do |to|
        receivers &lt;&lt; to.sub(/_\d+_p(er|re)@/, '@')
      end
      message.to = receivers
    end
  end
  </code></pre>
  
  <p>At first glance, this appears to work great. In the stage environment, the email is intercepted and the "to" address becomes the email address I want the email to go to. The person managing the sandbox account used to make the purchase receives the email.  Perfect... except the name of the sandbox account is gone. What once was <code>"Joe Example" &lt;joe_1338142567_per@example.com&gt;</code> changed to <code>"Joe Example" &lt;joe@example.com&gt;</code> is now changed to <code>joe@example.com</code></p>
  
  <p>...the name is now gone.</p>
  
  <p>Looking at the <a href="https://github.com/mikel/mail/blob/master/lib/mail/message.rb" rel="nofollow">Mail message interface</a>, I see that <code>message.to=</code> can be set with a name, but calling <code>message.to</code> gets me an array of just email addresses without names, whether the name was provided or not.</p>
  
  <p><strong>Question:</strong> what is the correct way to alter an email address without altering the name associated with the email address in a mail interceptor?</p>