jangomail-mailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,42 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+
14
+ # jeweler generated
15
+ pkg
16
+
17
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
18
+ #
19
+ # * Create a file at ~/.gitignore
20
+ # * Include files you want ignored
21
+ # * Run: git config --global core.excludesfile ~/.gitignore
22
+ #
23
+ # After doing this, these files will be ignored in all your git projects,
24
+ # saving you from having to 'pollute' every project you touch with them
25
+ #
26
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
27
+ #
28
+ # For MacOS:
29
+ #
30
+ #.DS_Store
31
+ #
32
+ # For TextMate
33
+ #*.tmproj
34
+ #tmtags
35
+ #
36
+ # For emacs:
37
+ #*~
38
+ #\#*
39
+ #.\#*
40
+ #
41
+ # For vim:
42
+ #*.swp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jangomail-mailer.gemspec
4
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jangomail-mailer (0.0.1)
5
+ mail
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.1.2)
11
+ fakeweb (1.3.0)
12
+ i18n (0.6.0)
13
+ mail (2.3.0)
14
+ i18n (>= 0.4.0)
15
+ mime-types (~> 1.16)
16
+ treetop (~> 1.4.8)
17
+ mime-types (1.16)
18
+ polyglot (0.3.2)
19
+ rspec (2.5.0)
20
+ rspec-core (~> 2.5.0)
21
+ rspec-expectations (~> 2.5.0)
22
+ rspec-mocks (~> 2.5.0)
23
+ rspec-core (2.5.1)
24
+ rspec-expectations (2.5.0)
25
+ diff-lcs (~> 1.1.2)
26
+ rspec-mocks (2.5.0)
27
+ treetop (1.4.10)
28
+ polyglot
29
+ polyglot (>= 0.3.1)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ fakeweb (~> 1.3.0)
36
+ jangomail-mailer!
37
+ rspec (~> 2.5.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jason Rust
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # JangoMail Transactional Mailer
2
+ Implements the [JangoMail Transactional API](http://api.jangomail.com/api.asmx?op=SendTransactionalEmail) as a
3
+ custom [mailer](https://github.com/mikel/mail) class.
4
+
5
+ ## Features
6
+ - Accepts JangoMail options (e.g. OpenTrack, ClickTrack, TransactionalGroupID, etc.)
7
+ - Sends both the HTML and plain text parts of the message to the JangoMail API.
8
+
9
+ ## Options
10
+ - `user_name` (required): your JangoMail user name
11
+ - `password` (required): your JangoMail password
12
+ - `options`: hash containing additional JangoMail options (e.g. `{ :OpenTrack => true }`)
13
+ - `api_url`: JangoMail API URL
14
+
15
+ ## Rails Examples
16
+ Put in `config/initializers/jangomail.rb`:
17
+
18
+ ActionMailer::Base.add_delivery_method :jangomail, Jangomail::Mailer,
19
+ :user_name => 'username',
20
+ :password => 'password',
21
+ :options => { :OpenTrack => true }
22
+
23
+ And in your ActionMailer class:
24
+
25
+ defaults :delivery_method => :jangomail
26
+
27
+ or for just a particular message:
28
+
29
+ def welcome(user) do
30
+ mail :to => user.email,
31
+ :delivery_method => :jangomail
32
+ end
33
+
34
+ ## Non-Rails Example
35
+ Create a mailer instance and deliver
36
+
37
+ mailer = Jangomail::Mailer.new :user_name => "username", :password => "password"
38
+ mail = Mail.new { ... }
39
+ mailer.deliver! mail
40
+
41
+ ## Contributors
42
+ - Jason Rust
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jangomail-mailer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jangomail-mailer"
7
+ s.version = Jangomail::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jason Rust"]
10
+ s.email = ["jason@rustedcode.com"]
11
+ s.homepage = "http://github.com/jrust/jangomail-mailer"
12
+ s.summary = %q{JangoMail mailer}
13
+ s.description = %q{Implements the JangoMail Transactional API as a custom mailer class.}
14
+
15
+ s.add_dependency "mail"
16
+ s.add_development_dependency "rspec", "~> 2.5.0"
17
+ s.add_development_dependency "fakeweb", "~> 1.3.0"
18
+
19
+ s.rubyforge_project = "jangomail-mailer"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,80 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'cgi'
4
+ require 'mail'
5
+
6
+ module Jangomail
7
+ class Mailer
8
+
9
+ def initialize(values = {})
10
+ self.settings = { :api_url => 'http://api.jangomail.com/api.asmx/SendTransactionalEmail',
11
+ :options => {},
12
+ :logger => defined?(Rails) && Rails.logger,
13
+ :log_level => :debug,
14
+ :user_name => nil,
15
+ :password => nil,
16
+ }.merge!(values)
17
+ raise ArgumentError, 'User Name needed' unless settings[:user_name]
18
+ raise ArgumentError, 'Password needed' unless settings[:password]
19
+ end
20
+
21
+ attr_accessor :settings
22
+
23
+ def new(*args)
24
+ self
25
+ end
26
+
27
+ def deliver!(mail)
28
+ uri = URI.parse(settings[:api_url])
29
+ if uri.scheme == 'https'
30
+ http = Net::HTTP.new(uri.host, 443)
31
+ http.use_ssl = true
32
+ else
33
+ http = Net::HTTP.new(uri.host)
34
+ end
35
+
36
+ destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
37
+ if destinations.blank?
38
+ raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message')
39
+ end
40
+
41
+ destinations.each do |destination|
42
+ data = request_data(mail, destination)
43
+ http.post(uri.path, data).body.tap do |response|
44
+ settings[:logger].send(settings[:log_level], "Jangomail: #{response}") if settings[:logger]
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def request_data(mail, destination)
52
+ # Set the envelope from to be either the return-path, the sender or the first from address
53
+ envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
54
+ if envelope_from.blank?
55
+ raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message')
56
+ end
57
+
58
+ option_str = settings[:options].inject([]) do |arr, option|
59
+ val = case option[1]
60
+ when TrueClass then 'True'
61
+ when FalseClass then 'False'
62
+ else option[1].to_s
63
+ end
64
+ arr << "#{option[0].to_s}=#{val}"
65
+ end.join(',')
66
+
67
+ {
68
+ 'Username' => settings[:user_name],
69
+ 'Password' => settings[:password],
70
+ 'FromEmail' => envelope_from,
71
+ 'FromName' => mail[:from].display_names.first,
72
+ 'ToEmailAddress'=> destination,
73
+ 'Subject' => mail.subject,
74
+ 'MessagePlain' => mail.multipart? ? mail.text_part.body.to_s : mail.body.to_s,
75
+ 'MessageHTML' => mail.multipart? ? mail.html_part.body.to_s : mail.body.to_s,
76
+ 'Options' => option_str,
77
+ }.map{ |k, v| "#{CGI.escape(k)}=#{CGI.escape(v)}"}.join('&')
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Jangomail
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'jangomail-mailer'
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jangomail::Mailer do
4
+ subject { Jangomail::Mailer.new(:user_name => 'abc', :password => '123', :options => { :OpenTrack => true }) }
5
+ let(:mail) do
6
+ Mail.new("From: \"Some Body\" <sender@example.com>\r\nTo: joe@example.com\r\nSubject: Hello\r\n\r\nHi there!").tap do |mail|
7
+ mail.html_part = Mail::Part.new do
8
+ content_type 'text/html; charset=UTF-8'
9
+ body '<h1>This is HTML</h1>'
10
+ end
11
+ end
12
+ end
13
+
14
+ before do
15
+ FakeWeb.register_uri :post, 'http://api.jangomail.com/api.asmx/SendTransactionalEmail', :body => 'Success'
16
+ end
17
+
18
+ describe 'settings' do
19
+ it 'requires user name' do
20
+ expect {
21
+ Jangomail::Mailer.new(:password => '123')
22
+ }.to raise_error(ArgumentError, 'User Name needed')
23
+ end
24
+
25
+ it 'requires password' do
26
+ expect {
27
+ Jangomail::Mailer.new(:user_name => 'abc')
28
+ }.to raise_error(ArgumentError, 'Password needed')
29
+ end
30
+
31
+ context 'when defaults are used' do
32
+ subject { Jangomail::Mailer.new(:user_name => 'abc', :password => '123').settings }
33
+ its([:api_url]) { should == 'http://api.jangomail.com/api.asmx/SendTransactionalEmail' }
34
+ its([:options]) { should == {} }
35
+ end
36
+
37
+ context 'when set by user' do
38
+ subject {
39
+ Jangomail::Mailer.new(
40
+ :user_name => 'abc',
41
+ :password => '123',
42
+ :api_url => 'http://www.example.com/foo',
43
+ :options => { :OpenTrack => false }
44
+ ).settings
45
+ }
46
+
47
+ its([:user_name]) { should == 'abc' }
48
+ its([:password]) { should == '123' }
49
+ its([:api_url]) { should == 'http://www.example.com/foo' }
50
+ its([:options]) { should == { :OpenTrack => false } }
51
+ end
52
+ end
53
+
54
+ describe '#deliver!' do
55
+ let(:mock_http) { mock('http', :body => 'Success') }
56
+
57
+ before do
58
+ Net::HTTP.stub!(:new).and_return mock_http
59
+ end
60
+
61
+ it 'posts the message to the API with the required parameters' do
62
+ post = "FromEmail=sender%40example.com&Options=OpenTrack%3DTrue&Subject=Hello&Username=abc&FromName=Some+Body&MessagePlain=Hi+there%21&MessageHTML=%3Ch1%3EThis+is+HTML%3C%2Fh1%3E&Password=123&ToEmailAddress=joe%40example.com"
63
+ mock_http.should_receive(:post).with('/api.asmx/SendTransactionalEmail', post).and_return(mock(:body => 'Success'))
64
+ subject.deliver!(mail)
65
+ end
66
+ end
67
+
68
+ context 'using Rails' do
69
+ it 'logs response when is available' do
70
+ Object.const_set 'Rails', Module.new
71
+ ::Rails.stub :logger => mock(:logger)
72
+
73
+ ::Rails.logger.should_receive(:debug).with('Jangomail: Success')
74
+ subject.deliver!(mail)
75
+
76
+ Object.class_eval { remove_const 'Rails' }
77
+ end
78
+
79
+ it 'does not raise exception when is not available' do
80
+ expect { subject.deliver!(mail) }.to_not raise_error
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'jangomail-mailer'
5
+ require 'fakeweb'
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jangomail-mailer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jason Rust
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-28 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mail
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 27
44
+ segments:
45
+ - 2
46
+ - 5
47
+ - 0
48
+ version: 2.5.0
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: fakeweb
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 27
60
+ segments:
61
+ - 1
62
+ - 3
63
+ - 0
64
+ version: 1.3.0
65
+ type: :development
66
+ version_requirements: *id003
67
+ description: Implements the JangoMail Transactional API as a custom mailer class.
68
+ email:
69
+ - jason@rustedcode.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .document
78
+ - .gitignore
79
+ - .rspec
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - jangomail-mailer.gemspec
86
+ - lib/jangomail-mailer.rb
87
+ - lib/jangomail-mailer/version.rb
88
+ - rails/init.rb
89
+ - spec/jangomail-mailer_spec.rb
90
+ - spec/spec_helper.rb
91
+ has_rdoc: true
92
+ homepage: http://github.com/jrust/jangomail-mailer
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options: []
97
+
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project: jangomail-mailer
121
+ rubygems_version: 1.4.2
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: JangoMail mailer
125
+ test_files:
126
+ - spec/jangomail-mailer_spec.rb
127
+ - spec/spec_helper.rb