Permalink
Browse files

Update staging and production email delivery

* Use recipient_interceptor gem.
* Use the Mail gem's interceptor feature.
* Separate SMTP from intercepting responsibilities.
* Set up SMTP for production and staging.

http://robots.thoughtbot.com/post/40822987615/delivering-all-email-from-staging-to-a-group-email
  • Loading branch information...
1 parent 704a47b commit 44c01dbcd3b3a1eca9d15599b23bbfa4228148f6 @croaky croaky committed Jan 24, 2013
@@ -33,11 +33,26 @@ def test_factories_first
append_file 'Rakefile', factories_spec_rake_task
end
+ def configure_smtp
+ copy_file 'smtp.rb', 'config/initializers/smtp.rb'
+
+ prepend_file 'config/environments/production.rb',
+ "require Rails.root.join('config/initializers/smtp')"
+
+ config = <<-RUBY
+ config.action_mailer.delivery_method = :smtp
+ config.action_mailer.smtp_settings = SMTP_SETTINGS
+ RUBY
+
+ inject_into_file 'config/environments/production.rb', config,
+ :after => 'config.action_mailer.raise_delivery_errors = false'
+ end
+
def setup_staging_environment
run 'cp config/environments/production.rb config/environments/staging.rb'
- inject_into_file 'config/environments/staging.rb',
- "\n config.action_mailer.delivery_method = :override_recipient_smtp, to: 'staging@example.com'",
- :after => 'config.action_mailer.raise_delivery_errors = false'
+
+ prepend_file 'config/environments/staging.rb',
+ "Mail.register_interceptor RecipientInterceptor.new(ENV['EMAIL_RECIPIENTS'])"
end
def initialize_on_precompile
@@ -231,10 +246,6 @@ def create_github_repo(repo_name)
run "#{path_addition} hub create #{repo_name}"
end
- def copy_libraries
- copy_file 'override_recipient_smtp.rb', 'lib/override_recipient_smtp.rb'
- end
-
def copy_miscellaneous_files
copy_file 'errors.rb', 'config/initializers/errors.rb'
end
@@ -25,14 +25,14 @@ def suspenders_customization
invoke :customize_gemfile
invoke :setup_development_environment
invoke :setup_test_environment
+ invoke :setup_production_environment
invoke :setup_staging_environment
invoke :create_suspenders_views
invoke :create_common_javascripts
invoke :add_jquery_ui
invoke :setup_database
invoke :configure_app
invoke :setup_stylesheets
- invoke :copy_libraries
invoke :copy_miscellaneous_files
invoke :customize_error_pages
invoke :remove_routes_comment_lines
@@ -64,6 +64,11 @@ def setup_test_environment
build :setup_guard_spork
end
+ def setup_production_environment
+ say 'Setting up the production environment'
+ build :configure_smtp
+ end
+
def setup_staging_environment
say 'Setting up the staging environment'
build :setup_staging_environment
View
@@ -9,6 +9,7 @@ gem 'pg'
gem 'psych'
gem 'rack-timeout'
gem 'rails', '>= 3.2.11'
+gem 'recipient_interceptor'
gem 'simple_form'
gem 'strong_parameters'
gem 'thin'
@@ -1,38 +0,0 @@
-module Mail
- # == Sending Email with Override Recipient SMTP
- #
- # Use the OverrideRecipientSMTP delivery method when you don't want your app
- # to accidentally send emails to addresses other than the overridden recipient
- # which you configure.
- #
- # An typical use case is in your app's staging environment, your development
- # team will receive all staging emails without accidentally emailing users with
- # active email addresses in the database.
- #
- # === Sending via OverrideRecipientSMTP
- #
- # config.action_mailer.delivery_method = :override_recipient_smtp,
- # to: 'staging@example.com'
- #
- # === Sending to multiple email addresses
- #
- # config.action_mailer.delivery_method = :override_recipient_smtp,
- # to: ['dan@example.com', 'harlow@example.com']
- class OverrideRecipientSMTP < Mail::SMTP
- def initialize(values)
- unless values[:to]
- raise ArgumentError.new('A :to option is required when using :override_recipient_smtp')
- end
-
- super(values)
- end
-
- def deliver!(mail)
- mail.to = settings[:to]
- mail.cc = nil
- mail.bcc = nil
-
- super(mail)
- end
- end
-end
View
@@ -0,0 +1,10 @@
+if Rails.env.staging? || Rails.env.production?
+ SMTP_SETTINGS = {
+ address: ENV['SMTP_ADDRESS'], # example: 'smtp.sendgrid.net'
+ authentication: :plain,
+ domain: ENV['SMTP_DOMAIN'], # example: 'this-app.com'
+ password: ENV['SMTP_PASSWORD'],
+ port: '587',
+ user_name: ENV['SMTP_USERNAME']
+ }
+end

0 comments on commit 44c01db

Please sign in to comment.