email_campaign 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/.gitignore +17 -0
  2. data/.rvmrc +19 -0
  3. data/Gemfile +17 -0
  4. data/MIT-LICENSE.txt +20 -0
  5. data/README.rdoc +36 -0
  6. data/Rakefile +40 -0
  7. data/app/assets/images/.gitkeep +0 -0
  8. data/app/assets/javascripts/application.js +0 -0
  9. data/app/assets/stylesheets/application.css +0 -0
  10. data/app/controllers/email_campaigns_controller.rb +49 -0
  11. data/app/helpers/.gitkeep +0 -0
  12. data/app/models/email_campaign.rb +25 -0
  13. data/app/models/email_campaign_recipient.rb +102 -0
  14. data/app/views/email_campaigns/index.html.erb +36 -0
  15. data/config/routes.rb +10 -0
  16. data/db/migrate/20130111224331_create_email_campaigns.rb +23 -0
  17. data/db/migrate/20130111225431_create_email_campaign_recipients.rb +46 -0
  18. data/email_campaign.gemspec +35 -0
  19. data/lib/email_campaign/engine.rb +18 -0
  20. data/lib/email_campaign/version.rb +3 -0
  21. data/lib/email_campaign.rb +16 -0
  22. data/script/rails +8 -0
  23. data/test/dummy/README.rdoc +261 -0
  24. data/test/dummy/Rakefile +7 -0
  25. data/test/dummy/app/assets/javascripts/application.js +15 -0
  26. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  27. data/test/dummy/app/controllers/application_controller.rb +3 -0
  28. data/test/dummy/app/helpers/application_helper.rb +2 -0
  29. data/test/dummy/app/mailers/.gitkeep +0 -0
  30. data/test/dummy/app/models/.gitkeep +0 -0
  31. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/test/dummy/config/application.rb +59 -0
  33. data/test/dummy/config/boot.rb +10 -0
  34. data/test/dummy/config/database.yml +25 -0
  35. data/test/dummy/config/environment.rb +5 -0
  36. data/test/dummy/config/environments/development.rb +37 -0
  37. data/test/dummy/config/environments/production.rb +67 -0
  38. data/test/dummy/config/environments/test.rb +37 -0
  39. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/test/dummy/config/initializers/inflections.rb +15 -0
  41. data/test/dummy/config/initializers/mime_types.rb +5 -0
  42. data/test/dummy/config/initializers/secret_token.rb +7 -0
  43. data/test/dummy/config/initializers/session_store.rb +8 -0
  44. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  45. data/test/dummy/config/locales/en.yml +5 -0
  46. data/test/dummy/config/routes.rb +4 -0
  47. data/test/dummy/config.ru +4 -0
  48. data/test/dummy/db/test.sqlite3 +0 -0
  49. data/test/dummy/lib/assets/.gitkeep +0 -0
  50. data/test/dummy/log/.gitkeep +0 -0
  51. data/test/dummy/log/test.log +0 -0
  52. data/test/dummy/public/404.html +26 -0
  53. data/test/dummy/public/422.html +26 -0
  54. data/test/dummy/public/500.html +25 -0
  55. data/test/dummy/public/favicon.ico +0 -0
  56. data/test/dummy/script/rails +6 -0
  57. data/test/email_campaign_test.rb +15 -0
  58. data/test/integration/navigation_test.rb +10 -0
  59. data/test/test_helper.rb +15 -0
  60. metadata +205 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
17
+ Gemfile.lock
data/.rvmrc ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env bash
2
+
3
+ ruby_string="ruby-1.9.3-p392"
4
+ gemset_name="email_campaign"
5
+
6
+ alias rails='bundle exec rails'
7
+
8
+ if rvm list strings | grep -q "${ruby_string}" ; then
9
+ # Load or create the specified environment
10
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
11
+ && -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" ]] ; then
12
+ \. "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}"
13
+ else
14
+ rvm --create "${ruby_string}@${gemset_name}"
15
+ fi
16
+ else
17
+ # Notify the user to install the desired interpreter before proceeding.
18
+ echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory."
19
+ fi
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in imagine_cms.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+
11
+ # Declare any dependencies that are still in development here instead of in
12
+ # your gemspec. These might include edge Rails or gems from your path or
13
+ # Git. Remember to move these dependencies to your gemspec before releasing
14
+ # your gem to rubygems.org.
15
+
16
+ # To use debugger
17
+ # gem 'debugger'
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Aaron Namba <aaron@biggerbird.com>
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.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ =email_campaign
2
+
3
+ For mailings related to Rails apps, email_campaign is designed to be a more flexible, more integrated (yet well-abstracted) alternative to email campaign services like Campaign Monitor and MailChimp. Designed to use SendGrid for delivery, but this is easy changed.
4
+
5
+ Does its best to prevent clients from sending too frequently and ruining your sender reputation, but no guarantees.
6
+
7
+ =Roadmap
8
+
9
+ 0.x: Basic campaign authoring and delivery, using template created by developer as a standard Rails mailer layout.
10
+ 1.0: Robust enough for production use, will be able to deal with 90% of common failure modes on its own.
11
+ 1.2: Delivery stats and graphs.
12
+
13
+ =Current Status
14
+
15
+ Still pre-1.0. If you have some time, write some code or tests, exercise existing functionality on non-critical apps, submit bug reports... otherwise stay away for now.
16
+
17
+ =Getting Help
18
+
19
+ Get paid support for EmailCampaign straight from the people who made it: {Bigger Bird Creative, Inc.}[https://www.biggerbird.com] Not required, of course. :-) Free support is up top (Issues).
20
+
21
+ =Customizing & Contributing
22
+
23
+ Pull requests always appreciated (recommend getting in touch first). If companies or individuals are willing to sponsor major features on the roadmap (or features that meet their own needs) development can proceed more quickly.
24
+
25
+ =Dependencies
26
+
27
+ Developed and tested with MRI ruby 1.9.3.
28
+
29
+ Dependencies:
30
+ * mail (email)
31
+
32
+ =License & Copyright
33
+
34
+ Distributed under MIT license. Copyright (c) 2013 Aaron Namba <aaron@biggerbird.com>
35
+
36
+ {<img src="https://travis-ci.org/anamba/email_campaign.png" />}[https://travis-ci.org/anamba/email_campaign]
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'EmailCampaign'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
File without changes
File without changes
File without changes
@@ -0,0 +1,49 @@
1
+ class EmailCampaignsController < ApplicationController
2
+ include EmailCampaignsAuthenticator
3
+ before_filter :check_permissions
4
+
5
+ # define authenticate in your initializer (in main app)
6
+
7
+ def check_permissions
8
+ render :action => 'permission_denied' unless authenticate
9
+ end
10
+
11
+ def index
12
+ # @drafts = EmailCampaign.where(:queued => false)
13
+ # @queued = EmailCampaign.where(:queued => true, :delivered => false)
14
+ # @sent = EmailCampaign.where(:delivered => true).order('delivery_finished_at desc')
15
+ end
16
+
17
+ def new
18
+
19
+ end
20
+
21
+ def create
22
+
23
+ end
24
+
25
+ def show
26
+
27
+ end
28
+
29
+ def edit
30
+
31
+ end
32
+
33
+ def update
34
+
35
+ end
36
+
37
+ def destroy
38
+
39
+ end
40
+
41
+ def deliver
42
+
43
+ end
44
+
45
+ def status
46
+
47
+ end
48
+
49
+ end
File without changes
@@ -0,0 +1,25 @@
1
+ class EmailCampaign < ActiveRecord::Base
2
+ attr_accessible :name, :mailer, :method, :params_yaml, :deliver_at
3
+ :finalized, :queued, :delivered
4
+
5
+ has_many :recipients, :class_name => 'EmailCampaignRecipient'
6
+
7
+ # new_recipients should be an Array of objects that respond to #email, #name, and #subscriber_id
8
+ # (falls back to #id if #subscriber_id doesn't exist; either way, this id should be unique)
9
+ def queue(new_recipients, limit = nil)
10
+ count = 0
11
+ while rcpt = new_recipients.shift do
12
+ next unless recipients.where(:subscriber_id => rcpt.subscriber_id).count == 0
13
+
14
+ rcpt.name.strip!
15
+ rcpt.email_address.strip!
16
+
17
+ r = recipients.create(:name => rcpt.name, :email_address => rcpt.email_address,
18
+ :subscriber_class_name => rcpt.class.name, :subscriber_id => rcpt.subscriber_id || rcpt.id)
19
+
20
+ r.queue if count < limit
21
+ count += 1
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,102 @@
1
+ class EmailCampaignRecipient < ActiveRecord::Base
2
+ attr_accessible :name, :email_address,
3
+ :ready, :duplicate, :invalid_email, :unsubscribed,
4
+ :subscriber_class_name, :subscriber_id
5
+
6
+ belongs_to :email_campaign
7
+
8
+ before_save :check_name, :check_for_duplicates, :check_email_address, :check_for_unsubscribe
9
+
10
+ def check_name
11
+ self.name = nil if name.blank?
12
+ end
13
+
14
+ def check_for_duplicates
15
+ if self.class.where(:campaign_id => campaign_id, :email_address => rcpt.email_address).count > 0
16
+ self.ready = false
17
+ self.duplicate = true
18
+ else
19
+ self.duplicate = false
20
+ end
21
+ end
22
+
23
+ def check_email_address
24
+ if valid_email_address?(rcpt.email_address)
25
+ self.invalid_email = false
26
+ else
27
+ self.ready = false
28
+ self.invalid_email = true
29
+ end
30
+ end
31
+
32
+ def check_for_unsubscribe
33
+ if self.class.where(:email_address => rcpt.email_address, :unsubscribed => true).count > 0
34
+ self.unsubscribed = true
35
+ self.ready = false
36
+ else
37
+ self.unsubscribed = false
38
+ end
39
+ end
40
+
41
+ def queue
42
+ if !duplicate && !invalid_email && !unsubscribed
43
+ update_attributes(:ready => true)
44
+ else
45
+ false
46
+ end
47
+ end
48
+
49
+ def deliver
50
+ # if we want to allow retries in the future we can change this bit
51
+ if attempted && attempts > 0
52
+ puts "Already attempted, not going to try again."
53
+ return false
54
+ end
55
+
56
+ if failed
57
+ puts "Already failed (reason: #{failure_reason}), not going to try again."
58
+ return false
59
+ end
60
+
61
+ unless update_column(:attempted, true) && increment(:attempts)
62
+ print "Could not update 'attempted' flag, will not proceed for fear of sending multiple copies"
63
+ return false
64
+ end
65
+
66
+ if email_address !~ /^[\w\d]+([\w\d\!\#\$\%\&\*\+\-\/\=\?\^\`\{\|\}\~\.]*[\w\d]+)*@([-\w\d]+\.)+[\w]{2,}$/
67
+ print "Invalid email address: #{email_address}"
68
+ self.failed = true
69
+ self.failure_reason = "Invalid email address"
70
+ save
71
+ return false
72
+ end
73
+
74
+ # TODO: wrap this with begin;rescue;end and set failed/failure_reason in case of exception
75
+ Mailer.email_campaign(self).deliver
76
+
77
+ true
78
+ end
79
+
80
+ def to_s
81
+ name.blank? ? email_address : "#{name} <#{email_address}>"
82
+ end
83
+
84
+ def valid_email_address?(value)
85
+ begin
86
+ m = Mail::Address.new(value)
87
+ # We must check that value contains a domain and that value is an email address
88
+ r = m.domain && m.address == value
89
+ t = m.__send__(:tree)
90
+ # We need to dig into treetop
91
+ # A valid domain must have dot_atom_text elements size > 1
92
+ # user@localhost is excluded
93
+ # treetop must respond to domain
94
+ # We exclude valid email values like <user@localhost.com>
95
+ # Hence we use m.__send__(tree).domain
96
+ r &&= (t.domain.dot_atom_text.elements.size > 1)
97
+ rescue Exception => e
98
+ false
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,36 @@
1
+ <h1>Email Campaigns</h1>
2
+
3
+ <%= flash_message %>
4
+
5
+ <h2>Drafts</h2>
6
+ <%- if @drafts.empty? -%>
7
+ No drafts.
8
+ <%- else -%>
9
+ <ul>
10
+ <%- @drafts.each do |e| -%>
11
+ <li><%= link_to e.name, e %></li>
12
+ <%- end -%>
13
+ </ul>
14
+ <%- end -%>
15
+
16
+ <h2>Queued for Sending</h2>
17
+ <%- if @queued.empty? -%>
18
+ Nothing queued.
19
+ <%- else -%>
20
+ <ul>
21
+ <%- @queued.each do |e| -%>
22
+ <li><%= link_to e.name, e %></li>
23
+ <%- end -%>
24
+ </ul>
25
+ <%- end -%>
26
+
27
+ <h3>Recently Sent</h3>
28
+ <%- if @sent.empty? -%>
29
+ Nothing sent recently.
30
+ <%- else -%>
31
+ <ul>
32
+ <%- @sent.each do |e| -%>
33
+ <li><%= link_to e.name, e %></li>
34
+ <%- end -%>
35
+ </ul>
36
+ <%- end -%>
data/config/routes.rb ADDED
@@ -0,0 +1,10 @@
1
+ Rails.application.routes.draw do
2
+
3
+ resources :email_campaigns do
4
+ member do
5
+ post :deliver
6
+ get :status
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,23 @@
1
+ class CreateEmailCampaigns < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table :email_campaigns do |t|
5
+ t.string :name
6
+
7
+ t.string :mailer
8
+ t.string :method
9
+ t.text :params_yaml
10
+
11
+ t.datetime :deliver_at
12
+
13
+ t.boolean :finalized, :default => false, :null => false
14
+ t.boolean :queued, :default => false, :null => false
15
+ t.boolean :delivered, :default => false, :null => false
16
+ t.datetime :delivery_started_at
17
+ t.datetime :delivery_finished_at
18
+
19
+ t.timestamps
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,46 @@
1
+ class CreateEmailCampaignRecipients < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table :email_campaign_recipients do |t|
5
+ t.integer :email_campaign_id, :null => false
6
+
7
+ t.string :email_address
8
+ t.string :name
9
+
10
+ t.string :subscriber_class_name
11
+ t.string :subscriber_id
12
+
13
+ t.string :status
14
+
15
+ t.boolean :ready, :default => false, :null => false
16
+
17
+ t.boolean :duplicate, :default => false, :null => false
18
+ t.boolean :invalid_email, :default => false, :null => false
19
+ t.boolean :unsubscribed, :default => false, :null => false
20
+
21
+ t.boolean :attempted, :default => false, :null => false
22
+ t.integer :attempts, :default => 0, :null => false
23
+ t.datetime :attempted_at
24
+
25
+ t.boolean :failed, :default => false, :null => false
26
+ t.datetime :failed_at
27
+ t.string :failure_reason
28
+
29
+ t.boolean :delivered, :default => false, :null => false
30
+ t.datetime :delivered_at
31
+
32
+ t.boolean :opened, :default => false, :null => false
33
+ t.datetime :opened_at
34
+ t.integer :opens, :default => 0, :null => false
35
+
36
+ t.boolean :clicked, :default => false, :null => false
37
+ t.datetime :clicked_at
38
+ t.integer :clicks, :default => 0, :null => false
39
+
40
+ t.timestamps
41
+ end
42
+ add_index :email_campaign_recipients, :email_address
43
+ add_index :email_campaign_recipients, :subscriber_id
44
+ end
45
+
46
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ # Maintain your gem's version:
5
+ require "email_campaign/version"
6
+
7
+ # Describe your gem and declare its dependencies:
8
+ Gem::Specification.new do |s|
9
+ s.name = "email_campaign"
10
+ s.version = EmailCampaign::VERSION
11
+ s.platform = Gem::Platform::RUBY
12
+ s.author = "Aaron Namba"
13
+ s.email = "aaron@biggerbird.com"
14
+ s.homepage = "https://github.com/anamba/email_campaign"
15
+ s.summary = %q{Email campaign delivery for Rails apps}
16
+ s.description = %q{See README for details.}
17
+
18
+ s.required_ruby_version = '>= 1.9.3'
19
+ s.required_rubygems_version = '>= 1.8.11'
20
+
21
+ s.license = 'MIT'
22
+
23
+ # s.rubyforge_project = "email_campaign"
24
+
25
+ s.files = `git ls-files`.split("\n")
26
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
27
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
28
+ s.require_paths = ["lib"]
29
+
30
+ s.add_dependency "actionmailer", "~> 3.2.12"
31
+ s.add_dependency "mail", "~> 2.4.4"
32
+ s.add_dependency "net-dns", "~> 0.7.1"
33
+
34
+ s.add_development_dependency "sqlite3"
35
+ end
@@ -0,0 +1,18 @@
1
+ module EmailCampaign
2
+
3
+ class Engine < Rails::Engine
4
+ engine_name 'email_campaign'
5
+
6
+ config.app_root = root
7
+ middleware.use ::ActionDispatch::Static, "#{root}/public"
8
+
9
+ # initializer "email_campaign.assets.precompile" do |config|
10
+ # Rails.application.config.assets.precompile += %w( codepress/** dojo/** management.css reset.css )
11
+ # end
12
+
13
+ #
14
+ # activate gems as needed
15
+ #
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module EmailCampaign
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "active_support/dependencies"
2
+
3
+ module EmailCampaign
4
+ # Our host application root path
5
+ # We set this when the engine is initialized
6
+ mattr_accessor :app_root
7
+
8
+ # Yield self on setup for nice config blocks
9
+ def self.setup
10
+ yield self
11
+ end
12
+
13
+ end
14
+
15
+ # Require our engine
16
+ require "email_campaign/engine"
data/script/rails ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/imagine_cms/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'