Permalink
Browse files

extend actionmailer to allow one smtp account per mailer class.

  • Loading branch information...
0 parents commit 87db143025bcb2879344bce128a4589f584d81fe @flyerhzm committed Mar 19, 2012
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in multiple_mailers.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Richard Huang (flyerhzm@gmail.com)
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
+# MultipleMailers
+
+extend actionmailer to allow one smtp account per mailer class.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'multiple_mailers'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install multiple_mailers
+
+## Usage
+
+define your smtp mailer accounts in `config/mailers.yml`
+
+ default: &default
+ address: 'smtp.gmail.com'
+ port: 587
+ domain: 'railsbp.com'
+ authentication: 'plain'
+
+ notification:
+ <<: *default
+ user_name: 'notification@railsbp.com'
+ password: 'password'
+
+ exception.notifier:
+ <<: *default
+ user_name: 'exception.notifier@railsbp.com'
+ password: 'password'
+
+the default account is used for all mailer classes, if you want to
+override it, you can define mailer account for any mailer class you
+want, like
+
+ class NotificationMailer < ActionMailer::Base
+ mailer_account "notification"
+ end
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,33 @@
+require "multiple_mailers/version"
+require "yaml"
+require "action_mailer"
+
+module MultipleMailers
+ autoload :Configuration, "multiple_mailers/configuration"
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.class_eval do
+ Configuration.load
+ mailer_account("default")
+ end
+ end
+
+ if defined? Rails::Railtie
+ class BulletRailtie < Rails::Railtie
+ initializer "multiple_mailers.configure_rails_initialization" do |app|
+ ActionMailer::Base.send(:include, MultipleMailers)
+ end
+ end
+ end
+
+ module ClassMethods
+ def mailer_account(name)
+ config = Configuration.get(name)
+ if config
+ self.smtp_settings = config.symbolize_keys
+ end
+ end
+ end
+end
+
@@ -0,0 +1,15 @@
+require "yaml"
+
+module MultipleMailers
+ class Configuration
+ class <<self
+ def load
+ @configurations ||= YAML.load_file(Rails.root.join("config/mailers.yml"))
+ end
+
+ def get(name)
+ @configurations and @configurations[name]
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module MultipleMailers
+ VERSION = "1.0.0"
+end
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/multiple_mailers/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Richard Huang"]
+ gem.email = ["flyerhzm@gmail.com"]
+ gem.description = %q{extend actionmailer to allow one smtp account per mailer class}
+ gem.summary = %q{extend actionmailer to allow one smtp account per mailer class}
+ gem.homepage = "https://github.com/flyerhzm/multiple_mailers"
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "multiple_mailers"
+ gem.require_paths = ["lib"]
+ gem.version = MultipleMailers::VERSION
+
+ gem.add_development_dependency "actionmailer"
+end
@@ -0,0 +1,15 @@
+default: &default
+ address: 'smtp.gmail.com'
+ port: 587
+ domain: 'railsbp.com'
+ authentication: 'plain'
+
+notification:
+ <<: *default
+ user_name: 'notification@railsbp.com'
+ password: 'password'
+
+exception.notifier:
+ <<: *default
+ user_name: 'exception.notifier@railsbp.com'
+ password: 'password'
@@ -0,0 +1,36 @@
+require 'spec_helper'
+
+module MultipleMailers
+ describe Configuration do
+ context ".load" do
+ it "should read config/mailers.yml" do
+ Configuration.load
+ Configuration.instance_variable_get(:@configurations).should_not be_empty
+ end
+ end
+
+ context ".get" do
+ before { Configuration.load }
+
+ it "should get configuration for notification" do
+ config = Configuration.get("notification")
+ config["address"].should == "smtp.gmail.com"
+ config["port"].should == 587
+ config["domain"].should == "railsbp.com"
+ config["authentication"].should == "plain"
+ config["user_name"].should == "notification@railsbp.com"
+ config["password"].should == "password"
+ end
+
+ it "should get configuration for exception_notifier" do
+ config = Configuration.get("exception.notifier")
+ config["address"].should == "smtp.gmail.com"
+ config["port"].should == 587
+ config["domain"].should == "railsbp.com"
+ config["authentication"].should == "plain"
+ config["user_name"].should == "exception.notifier@railsbp.com"
+ config["password"].should == "password"
+ end
+ end
+ end
+end
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+describe MultipleMailers do
+ context ".mail_account" do
+ before { ActionMailer::Base.send(:include, MultipleMailers) }
+
+ it "should set default configuration" do
+ default_config = ActionMailer::Base.smtp_settings
+ default_config[:address].should == "smtp.gmail.com"
+ default_config[:port].should == 587
+ default_config[:domain].should == "railsbp.com"
+ default_config[:authentication].should == "plain"
+ default_config[:user_name].should be_nil
+ default_config[:password].should be_nil
+ end
+
+ it "should set notification configuration" do
+ ActionMailer::Base.class_eval { mailer_account "notification" }
+ default_config = ActionMailer::Base.smtp_settings
+ default_config[:address].should == "smtp.gmail.com"
+ default_config[:port].should == 587
+ default_config[:domain].should == "railsbp.com"
+ default_config[:authentication].should == "plain"
+ default_config[:user_name].should == "notification@railsbp.com"
+ default_config[:password].should == "password"
+ end
+ end
+end
@@ -0,0 +1,9 @@
+require 'rspec'
+
+class Rails
+ def self.root
+ Pathname.new(__FILE__).dirname
+ end
+end
+
+require 'multiple_mailers'

0 comments on commit 87db143

Please sign in to comment.