Skip to content

Instantly share code, notes, and snippets.

@hungpk
Forked from apeckham/actionmailer_extensions.rb
Last active August 29, 2015 14:10
Show Gist options
  • Save hungpk/ba611248b16e0d3df99a to your computer and use it in GitHub Desktop.
Save hungpk/ba611248b16e0d3df99a to your computer and use it in GitHub Desktop.
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
require 'spec_helper'
class TestMailer < ActionMailer::Base
self.template_root = Rails.root.join("spec/mailer_views").to_s
def test_mandrill_smtp
from "me@example.com"
recipients "you@example.com"
end
def test_sendgrid_smtp
from "me@example.com"
recipients "you@example.com"
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
config.action_mailer.delivery_method = :from_header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment