Skip to content

Instantly share code, notes, and snippets.

@apeckham
Created December 19, 2012 08:28
Show Gist options
  • Select an option

  • Save apeckham/4335288 to your computer and use it in GitHub Desktop.

Select an option

Save apeckham/4335288 to your computer and use it in GitHub Desktop.

Revisions

  1. apeckham revised this gist Dec 19, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion actionmailer_extensions_spec.rb
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ def test_mandrill_smtp
    def test_sendgrid_smtp
    from "[email protected]"
    recipients "[email protected]"
    headers X_DELIVERY_METHOD => "sendgrid_smtp"
    headers "X-Delivery-Method" => "sendgrid_smtp"
    end
    end

  2. apeckham revised this gist Dec 19, 2012. 2 changed files with 37 additions and 0 deletions.
    File renamed without changes.
    37 changes: 37 additions & 0 deletions actionmailer_extensions_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    require 'spec_helper'

    class TestMailer < ActionMailer::Base
    self.template_root = Rails.root.join("spec/mailer_views").to_s

    def test_mandrill_smtp
    from "[email protected]"
    recipients "[email protected]"
    end

    def test_sendgrid_smtp
    from "[email protected]"
    recipients "[email protected]"
    headers X_DELIVERY_METHOD => "sendgrid_smtp"
    end
    end

    describe ActionMailer::Base do
    before do
    ActionMailer::Base.delivery_method = :from_header
    Net::SMTP.stubs(:new)
    end

    after { ActionMailer::Base.delivery_method = :test }

    it "delivers via mandrill" do
    TestMailer.any_instance.expects(:perform_delivery_smtp)
    TestMailer.deliver_test_mandrill_smtp
    TestMailer.smtp_settings[:address].should == "smtp.mandrillapp.com"
    end

    it "delivers via sendgrid" do
    TestMailer.any_instance.expects(:perform_delivery_smtp)
    TestMailer.deliver_test_sendgrid_smtp
    TestMailer.smtp_settings[:address].should == "smtp.sendgrid.infos"
    end
    end
  3. apeckham revised this gist Dec 19, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions production.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    config.action_mailer.delivery_method = :from_header
  4. apeckham created this gist Dec 19, 2012.
    24 changes: 24 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    module ActionMailer
    class Base
    def perform_delivery_from_header(tmail)
    method = tmail.header.delete("x-delivery-method") || :server_2
    send "perform_delivery_#{method}", tmail
    end

    def perform_delivery_server_1(tmail)
    self.class.smtp_settings = {
    address: "server1.smtp.com",
    port: 25
    }
    perform_delivery_smtp tmail
    end

    def perform_delivery_server_2(tmail)
    self.class.smtp_settings = {
    address: "server2.smtp.com",
    port: 587
    }
    perform_delivery_smtp tmail
    end
    end
    end