title: Sending emails during Capistrano deployment using Rails 5, Ruby 2.4, and Capistrano 3
body: <p>Capistrano 3.7.2, Rails 5.0.2, and Ruby 2.4.0 to be specific. The error I get occurs after <code>Execute load:defaults</code>:</p>
  
  <pre><code>** Execute load:defaults
  cap aborted!
  NameError: uninitialized constant ActiveSupport::Rescuable
  /Users/username/.rvm/gems/ruby-2.4.0/gems/actionmailer-5.0.2/lib/action_mailer/rescuable.rb:6:in `&lt;module:Rescuable&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/actionmailer-5.0.2/lib/action_mailer/rescuable.rb:4:in `&lt;module:ActionMailer&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/actionmailer-5.0.2/lib/action_mailer/rescuable.rb:1:in `&lt;top (required)&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/actionmailer-5.0.2/lib/action_mailer/base.rb:8:in `require'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/actionmailer-5.0.2/lib/action_mailer/base.rb:8:in `&lt;top (required)&gt;'
  /Users/username/rails_5_test_app/config/deploy/notify/cap_mailer.rb:5:in `require'
  /Users/username/rails_5_test_app/config/deploy/notify/cap_mailer.rb:5:in `&lt;top (required)&gt;'
  config/deploy.rb:4:in `require'
  config/deploy.rb:4:in `&lt;top (required)&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/capistrano-3.7.2/lib/capistrano/setup.rb:27:in `load'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/capistrano-3.7.2/lib/capistrano/setup.rb:27:in `block (3 levels) in &lt;top (required)&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/capistrano-3.7.2/lib/capistrano/configuration/variables.rb:32:in `untrusted!'
  /Users/username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/delegate.rb:83:in `method_missing'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/capistrano-3.7.2/lib/capistrano/setup.rb:26:in `block (2 levels) in &lt;top (required)&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/task.rb:250:in `block in execute'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/task.rb:250:in `each'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/task.rb:250:in `execute'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/task.rb:194:in `block in invoke_with_call_chain'
  /Users/username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/task.rb:187:in `invoke_with_call_chain'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/task.rb:180:in `invoke'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:152:in `invoke_task'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:108:in `each'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:108:in `block in top_level'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:117:in `run_with_threads'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:102:in `top_level'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:80:in `block in run'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
  /Users/username/.rvm/gems/ruby-2.4.0@global/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/capistrano-3.7.2/lib/capistrano/application.rb:14:in `run'
  /Users/username/.rvm/gems/ruby-2.4.0/gems/capistrano-3.7.2/bin/cap:3:in `&lt;top (required)&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0/bin/cap:22:in `load'
  /Users/username/.rvm/gems/ruby-2.4.0/bin/cap:22:in `&lt;main&gt;'
  /Users/username/.rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `eval'
  /Users/username/.rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `&lt;main&gt;'
  Tasks: TOP =&gt; staging
  </code></pre>
  
  <p>I'm about to start upgrading my Rails 4 apps to Rails 5, and decided to do a small test app to make sure all my gems are working. Everything is fine, except for the code I've been using since Rails 2 to send emails during a Capistrano deploy, which results in this error when I try to require or load the mailer code; line 4 of deploy.rb is the require. If I comment that out and set a flag to not send an email, the deploy works perfectly.</p>
  
  <p>The mailer code (based off of something posted by Mislav Marohnić years ago):</p>
  
  <p>cap_mailer.rb:</p>
  
  <pre><code>require "action_mailer"
  
  yaml_config = File.read("config/email.yml")
  email_config = YAML.load(yaml_config)
  ActionMailer::Base.delivery_method = email_config['delivery_method'] # currently set to :smtp
  ActionMailer::Base.smtp_settings = {
      address: email_config['smtp_settings']['address'],
      port: email_config['smtp_settings']['port'],
      notify_emails: email_config['notify_emails']
  }
  ActionMailer::Base.view_paths = File.dirname(__FILE__)
  
  class CapMailer &lt; ActionMailer::Base
    default :from =&gt; 'App Deployment &lt;app_deploy@my.org&gt;'
  
    def deploy_notification(cap_vars)
      @now = Time.now
  
      set :body, ENV['comment']
  
      mail(:to =&gt; fetch(:notify_emails),
           :subject =&gt; "#{fetch(:human_readable_application_name)} - Changes to application on #{fetch(:stage)} server at #{fetch(:host)}"
      )
    end
  
    def test_email
      @now = Time.now
  
      mail(:to =&gt; "developer@my.org",
           :subject =&gt; "#{fetch(:human_readable_application_name)} - Capistrano test email #{@now.strftime("on %m/%d/%Y at %I:%M %p")}"
      )
  
    end
  
  end
  </code></pre>
  
  <p>All I can dig up online are references to very similar mailing set-ups (e.g. <a href="https://gist.github.com/johnthethird/955917" rel="nofollow noreferrer">https://gist.github.com/johnthethird/955917</a>) that were written for older versions of everything. There are several gems written to send mail during Capistrano deploys, but they have the same problem with being really old (<a href="https://github.com/gofullstack/capistrano-notifier" rel="nofollow noreferrer">capistrano-notifier</a>, <a href="https://github.com/pboling/capistrano_mailer" rel="nofollow noreferrer">capistrano_mailer</a>).</p>
  
  <p>So at this point I'm stuck. Googling <code>NameError: uninitialized constant ActiveSupport::Rescuable</code> suggested maybe this is a config problem, but I don't know what to look for there. ActiveSupport::Rescuable IS there in my Rails installation; I double-checked. Any ideas would be appreciated.</p>