Search code examples
ruby-on-railsrakeactionmailerrake-task

ActionMailer w/ Custom Rake Task


I am trying to create two custom rake tasks in in Rails 4 app. They are supposed to read the agent_card table and based on the logic, fire off an email when the time is triggered. This is my first time creating a rake task and I am not getting an error, therefore I don't know if I am doing it right. I would assume when using the mailer that letter opener would work once the mailer is fired (should it all be working correctly), but I am not sure. I have posted all relevant code below. Thanks in advance!

.rake file

namespace :agent_cards do
   desc 'Sends an email to an agent with a license expiring 2 months from today'
   task license_expire_agent: :environment do
      Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
      AgentCard.all.each do |agent_card|
         if agent_card.real_estate_license_expires_at == Date.today + 2.months
            LicenseExpireMailer.license_expire_agent(@agent_card, @agent).deliver_later
         end
      end
   end
end

namespace :agent_cards do
   desc 'Sends an email to an the agent development manager when a license expires 1 week from today'
   task license_expire_mgr: :environment do
      Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
      AgentCard.all.each do |agent_card|
         if agent_card.real_estate_license_expires_at == Date.today + 7
            LicenseExpireMailer.license_expire_mgr(@agent_card, @agent).deliver_later
         end
      end
   end
end

license_expire_agent_mailer.rb

class LicenseExpireMailer < ActionMailer::Base
   default from: "Mike <[email protected]>"

   def license_expire_agent(agent, agent_card)
      @agent = agent
      @agent_card = agent_card
      mail to: "[email protected]", subject: 'Your license is about to expire!'
   end
 end

license_expire_mgr_mailer.rb

class LicenseExpireMailer < ActionMailer::Base
   default from: "Mike <[email protected]>"

   def license_expire_mgr(agent, agent_card)
      @agent = agent
      @agent_card = agent_card
      # Head of Agent Development
      mail to: "[email protected]", subject: "#{@agent.name}'s License Expiring"
   end
end

When I try to test the rake task I enter

rake agent_cards:license_expire_agent

or

rake agent_cards:license_expire_mgr

Error

NameError: uninitialized constant LicenseExpireMailer
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:7:in `block (3      levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activerecord-   4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:5:in `block (2 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in  `<top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => agent_cards:license_expire_agent

Solution

  • Your mailer file name does not match with the mailer class name as per Rails convention.

    Mailer class should be placed in this path:

    app/mailers/license_expire_mailer.rb