mail_male_mail 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mail_male_mail.gemspec
4
+ gemspec
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mail_male_mail (0.0.2)
5
+ actionmailer (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionmailer (3.2.13)
11
+ actionpack (= 3.2.13)
12
+ mail (~> 2.5.3)
13
+ actionpack (3.2.13)
14
+ activemodel (= 3.2.13)
15
+ activesupport (= 3.2.13)
16
+ builder (~> 3.0.0)
17
+ erubis (~> 2.7.0)
18
+ journey (~> 1.0.4)
19
+ rack (~> 1.4.5)
20
+ rack-cache (~> 1.2)
21
+ rack-test (~> 0.6.1)
22
+ sprockets (~> 2.2.1)
23
+ activemodel (3.2.13)
24
+ activesupport (= 3.2.13)
25
+ builder (~> 3.0.0)
26
+ activesupport (3.2.13)
27
+ i18n (= 0.6.1)
28
+ multi_json (~> 1.0)
29
+ builder (3.0.4)
30
+ columnize (0.3.6)
31
+ debugger (1.5.0)
32
+ columnize (>= 0.3.1)
33
+ debugger-linecache (~> 1.2.0)
34
+ debugger-ruby_core_source (~> 1.2.0)
35
+ debugger-linecache (1.2.0)
36
+ debugger-ruby_core_source (1.2.0)
37
+ diff-lcs (1.2.3)
38
+ erubis (2.7.0)
39
+ hike (1.2.2)
40
+ i18n (0.6.1)
41
+ journey (1.0.4)
42
+ mail (2.5.3)
43
+ i18n (>= 0.4.0)
44
+ mime-types (~> 1.16)
45
+ treetop (~> 1.4.8)
46
+ mime-types (1.22)
47
+ multi_json (1.7.2)
48
+ polyglot (0.3.3)
49
+ rack (1.4.5)
50
+ rack-cache (1.2)
51
+ rack (>= 0.4)
52
+ rack-test (0.6.2)
53
+ rack (>= 1.0)
54
+ rspec (2.13.0)
55
+ rspec-core (~> 2.13.0)
56
+ rspec-expectations (~> 2.13.0)
57
+ rspec-mocks (~> 2.13.0)
58
+ rspec-core (2.13.1)
59
+ rspec-expectations (2.13.0)
60
+ diff-lcs (>= 1.1.3, < 2.0)
61
+ rspec-mocks (2.13.1)
62
+ sprockets (2.2.2)
63
+ hike (~> 1.2)
64
+ multi_json (~> 1.0)
65
+ rack (~> 1.0)
66
+ tilt (~> 1.1, != 1.3.0)
67
+ tilt (1.3.7)
68
+ treetop (1.4.12)
69
+ polyglot
70
+ polyglot (>= 0.3.1)
71
+
72
+ PLATFORMS
73
+ ruby
74
+
75
+ DEPENDENCIES
76
+ debugger
77
+ mail_male_mail!
78
+ rspec
data/README.md CHANGED
@@ -1,4 +1,70 @@
1
1
  MailMaleMail
2
2
  ==============
3
3
 
4
- Manage mail configurations using configuration instead of code. This allows you to cleanly and easily swap mail settings.
4
+ Manage mail configurations using configuration instead of code. This allows you to cleanly and easily swap mail settings.
5
+
6
+
7
+ ## Usage
8
+
9
+ ### Config File
10
+
11
+ Create a config in config/mail_male_mail.yml file and specify your settings
12
+
13
+ ```
14
+ #config/mail_male_mail.yml
15
+ production: &production
16
+ mailman_1:
17
+ delivery_method: smtp
18
+ smtp_settings:
19
+ address: "smtp.gmail.com"
20
+ port: 587
21
+ development:
22
+ <%= "<<: *production" if ENV['SEND_REAL_EMAILS'] == '1' %>
23
+ default:
24
+ delivery_method: letter_opener
25
+ test:
26
+ default:
27
+ delivery_method: test
28
+ ```
29
+
30
+ ### Mailman
31
+
32
+ In your mailer specify the 'mailman' to deliever the mail:
33
+
34
+ ```
35
+ class MainMailer < ActionMailer::Base
36
+ mailman :mailman_1
37
+ end
38
+ ```
39
+
40
+ ### Categories/Variables
41
+
42
+ MailMaleMale supports Sendgrid Categories/UniqArgs and Mailgun Tags/Varibales
43
+
44
+ <table>
45
+ <tr><th>MailMaleMail</th><th>Sendgrid</th><th>Mailgun</th></tr>
46
+ <tr><td>category</td><td>category</td><td>tag</td></tr>
47
+ <tr><td>variables</td><td>unique args</td><td>variables</td></tr>
48
+ </table>
49
+
50
+ ```
51
+ class MainMailer < ActionMailer::Base
52
+ def test_email
53
+ set_mail_male_mail_category("CategoryName")
54
+ set_mail_male_mail_variables(:key1 => "value1", :key2 => "value2")
55
+ mail(...)
56
+ end
57
+ end
58
+ ```
59
+
60
+ To add this functionality, add sendgrid/mailgun to the provider in your config
61
+
62
+ ```
63
+ #config/mail_male_mail.yml
64
+ production:
65
+ sendgrid_mailman:
66
+ provider: sendgrid
67
+ mailgun_mailman:
68
+ provider: mailgun
69
+ ```
70
+
@@ -1,13 +1,19 @@
1
1
  require "yaml"
2
+ require 'json'
2
3
  require "action_mailer"
4
+ require "mail_male_mail/sendgrid"
5
+ require "mail_male_mail/mailgun"
6
+ require "mail_male_mail/configuration"
3
7
 
4
8
  module MailMaleMail
5
- autoload :Configuration, "mail_male_mail/configuration"
6
-
9
+ PROVIDERS = %w(sendgrid mailgun)
7
10
 
8
11
  if defined? Rails::Railtie
9
12
  class MailMaleMailRailtie < Rails::Railtie
10
13
  initializer "mail_male_mail_initializer" do |app|
14
+ unless File.exist? MailMaleMail::Configuration.filepath
15
+ raise LoadError, "#{MailMaleMail::Configuration.filepath} is required for MailMaleMail and does not exist"
16
+ end
11
17
  ActionMailer::Base.send(:include, MailMaleMail)
12
18
  end
13
19
  end
@@ -17,16 +23,46 @@ module MailMaleMail
17
23
  base.extend(ClassMethods)
18
24
  base.class_eval do
19
25
  Configuration.load
26
+ include(Sendgrid)
27
+ include(Mailgun)
28
+ class << self
29
+ attr_accessor :mmm_provider
30
+ end
31
+ attr_accessor :mmm_category, :mmm_variables
32
+ alias_method_chain :mail, :mmm_headers
33
+
20
34
  mailman('default')
21
35
  end
22
36
  end
23
37
 
38
+ def mail_with_mmm_headers(headers={}, &block)
39
+ mail_without_mmm_headers(headers, &block).tap do |m|
40
+ send("write_#{self.class.mmm_provider}_headers") if self.class.mmm_provider
41
+ end
42
+ end
43
+
44
+ def mail_male_mail_category(category)
45
+ self.mmm_category = category
46
+ end
47
+
48
+ def mail_male_mail_variables(variables)
49
+ if variables.is_a?(Hash)
50
+ self.mmm_variables = variables
51
+ else
52
+ raise TypeError, "variables must be a Hash"
53
+ end
54
+ end
55
+
24
56
  module ClassMethods
25
57
  def mailman(name)
26
- config = Configuration.get(name)
27
- if config
58
+ if config = Configuration.get(name)
28
59
  self.delivery_method = config['delivery_method'].to_sym if config.key?('delivery_method')
29
- self.smtp_settings = config['smtp_settings'].symbolize_keys if config.key?('smtp_settings') && config['smtp_settings'].is_a?(Hash)
60
+ if config.key?('smtp_settings') && config['smtp_settings'].is_a?(Hash)
61
+ self.smtp_settings = config['smtp_settings'].symbolize_keys
62
+ end
63
+ if config.key?('provider') && PROVIDERS.include?(config['provider'])
64
+ self.mmm_provider = config['provider']
65
+ end
30
66
  end
31
67
  end
32
68
  end
@@ -3,8 +3,11 @@ require "yaml"
3
3
  module MailMaleMail
4
4
  class Configuration
5
5
  class <<self
6
+ def filepath
7
+ @filepath ||= Rails.root.join("config/mail_male_mail.yml")
8
+ end
6
9
  def load
7
- @configurations ||= YAML.load(ERB.new(File.read(Rails.root.join("config/mail_male_mail.yml"))).result)[Rails.env]
10
+ @configurations ||= YAML.load(ERB.new(File.read(filepath)).result)[Rails.env]
8
11
  end
9
12
 
10
13
  def get(name)
@@ -0,0 +1,12 @@
1
+ module MailMaleMail
2
+ module Mailgun
3
+ def write_mailgun_headers
4
+ if mmm_category == :use_subject_lines
5
+ self.headers['X-Mailgun-Tag'] = message.subject
6
+ elsif mmm_category
7
+ self.headers['X-Mailgun-Tag'] = mmm_category
8
+ end
9
+ self.headers['X-Mailgun-Variables'] = mmm_variables.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3') if mmm_variables
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ module MailMaleMail
2
+ module Sendgrid
3
+ def write_sendgrid_headers
4
+ self.headers['X-SMTPAPI'] = build_sendgrid_json
5
+ end
6
+
7
+ private
8
+
9
+ def build_sendgrid_json
10
+ header_opts = {}
11
+
12
+ if mmm_category == :use_subject_lines
13
+ header_opts[:category] = message.subject
14
+ elsif mmm_category
15
+ header_opts[:category] = mmm_category
16
+ end
17
+ header_opts[:unique_args] = mmm_variables if mmm_variables
18
+
19
+ header_opts.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3')
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module MailMaleMail
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -14,5 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.require_paths = ["lib"]
15
15
  gem.version = MailMaleMail::VERSION
16
16
 
17
- gem.add_development_dependency "actionmailer"
17
+ gem.add_dependency "actionmailer", ">= 3.0.0"
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "debugger"
18
20
  end
@@ -0,0 +1,32 @@
1
+ production:
2
+ default: &default
3
+ delivery_method: test
4
+ smtp_settings:
5
+ address: 'smtp.gmail.com'
6
+ port: 587
7
+
8
+ invalid_provider:
9
+ <<: *default
10
+ provider: "google"
11
+ sendgrid:
12
+ <<: *default
13
+ provider: sendgrid
14
+ smtp_settings:
15
+ address: "smtp.sendgrid.net"
16
+ port: '587'
17
+ enable_starttls_auto: true
18
+ domain: "example.com"
19
+ authentication: 'plain'
20
+ user_name: "test@example.com"
21
+ password: "superamazingpasswordforsendgrid"
22
+ mailgun:
23
+ <<: *default
24
+ provider: mailgun
25
+ smtp_settings:
26
+ address: "smtp.mailgun.org"
27
+ port: '587'
28
+ enable_starttls_auto: true
29
+ user_name: "test@example.com"
30
+ password: "superamazingpasswordformailgun"
31
+
32
+
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ class MMMMailgunMailer < MMMailer
4
+ mailman :mailgun
5
+ def test_mail
6
+ set_mmm_cateogry("MailgunCategory1")
7
+ set_mmm_variables(:color => "green", :sound => "bark")
8
+ mail(:subject => "Mailgun Testing", :from => "test@example.com", :to => "test2@example.com")
9
+ end
10
+ end
11
+
12
+ module MailMaleMail
13
+ describe Sendgrid do
14
+ it "should set the X-SMTPAPI header with category unique_args" do
15
+ mail = MMMMailgunMailer.test_mail
16
+ mail.header['X-Mailgun-Tag'].to_s.should == "MailgunCategory1"
17
+ mail.header['X-Mailgun-Variables'].to_s.should == "{\"color\": \"green\", \"sound\": \"bark\"}"
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ class MMMSendGridMailer < MMMailer
4
+ mailman :sendgrid
5
+ def test_mail
6
+ set_mmm_cateogry("SendgridCategory1")
7
+ set_mmm_variables(:color => "blue", sound: "meow")
8
+ mail(:subject => "Sengrid Testing", :from => "test@example.com", :to => "test2@example.com")
9
+ end
10
+ end
11
+
12
+ module MailMaleMail
13
+ describe Sendgrid do
14
+ it "should set the X-SMTPAPI header with category unique_args" do
15
+ mail = MMMSendGridMailer.test_mail
16
+ mail.header['X-SMTPAPI'].to_s.should == "{\"category\": \"SendgridCategory1\", \"unique_args\": {\"color\": \"blue\", \"sound\": \"meow\"}}"
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe MailMaleMail do
4
+ it "should not ignore invalid providers" do
5
+ MMMailer.class_eval {mailman :default}
6
+ MMMailer.mmm_provider.should be_nil
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ require 'rspec'
2
+ require 'debugger'
3
+
4
+ class Rails
5
+ def self.root
6
+ Pathname.new(__FILE__).dirname
7
+ end
8
+
9
+ def self.env
10
+ "production"
11
+ end
12
+ end
13
+
14
+ require "mail_male_mail"
15
+ ActionMailer::Base.send(:include, MailMaleMail)
16
+ class MMMailer < ActionMailer::Base
17
+ end
18
+
19
+ RSpec.configure do |config|
20
+ config.after :each do
21
+ MMMailer.class_eval do
22
+ mailman :default
23
+ self.mmm_provider = nil
24
+ end
25
+ end
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_male_mail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,42 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-17 00:00:00.000000000 Z
12
+ date: 2013-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionmailer
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: debugger
16
48
  requirement: !ruby/object:Gem::Requirement
17
49
  none: false
18
50
  requirements:
@@ -35,12 +67,21 @@ extensions: []
35
67
  extra_rdoc_files: []
36
68
  files:
37
69
  - .gitignore
70
+ - Gemfile
71
+ - Gemfile.lock
38
72
  - README.md
39
73
  - Rakefile
40
74
  - lib/mail_male_mail.rb
41
75
  - lib/mail_male_mail/configuration.rb
76
+ - lib/mail_male_mail/mailgun.rb
77
+ - lib/mail_male_mail/sendgrid.rb
42
78
  - lib/mail_male_mail/version.rb
43
79
  - mail_male_mail.gemspec
80
+ - spec/config/mail_male_mail.yml
81
+ - spec/mail_male_mail/mailgun_spec.rb
82
+ - spec/mail_male_mail/sendgrid_spec.rb
83
+ - spec/mail_male_mail_spec.rb
84
+ - spec/spec_helper.rb
44
85
  homepage: http://github.com/carlallen/mail_male_mail
45
86
  licenses: []
46
87
  post_install_message:
@@ -65,4 +106,9 @@ rubygems_version: 1.8.23
65
106
  signing_key:
66
107
  specification_version: 3
67
108
  summary: extend actionmailer to work with multiple mail providers
68
- test_files: []
109
+ test_files:
110
+ - spec/config/mail_male_mail.yml
111
+ - spec/mail_male_mail/mailgun_spec.rb
112
+ - spec/mail_male_mail/sendgrid_spec.rb
113
+ - spec/mail_male_mail_spec.rb
114
+ - spec/spec_helper.rb