ruby-on-rails-4.2ruby-2.1postmark

Mailer method not being called?


inside controller:

def update  
  @user.update({approved: true})  
  UserMailer.send_approved_mail(@user)  
  redirect_to(root_url)  
end  

inside user_mailer.rb

class UserMailer < Devise::Mailer  
  def send_approved_mail(user)
    @resource = user
    email_body = render "user_activated_mail"
    if @resource.valid?
     client = Postmark::ApiClient.new(ENV["POSTMARK_API_KEY"])
     client.deliver(from: ENV["POSTMARK_SIGNATURE"], 
      to: @resource.email, subject: "User Activation information.", 
      tag: 'account-activated', :content_type => "text/html",
      html_body: email_body)
    end
  end
end  

In rails 4.1.0 above method in controller is being called and email is being sent, but in rails 4.2 the mailer method in controller is not being called, but when called from rails console it works. I have made all the necessary configuration for postmark api and in configuration files. The only thing is in rails 4.1.0 mailer inside controller gets called but in rails 4.2 it doesn't but works when called from rails console. What is the exact reason really can't figure it out.


Solution

  • The behavior you notice here is the new default introduced in Rails 4.2:

    With the introduction of Active Job and #deliver_later ... the invocation of the instance methods (on a mailer) is deferred until either deliver_now or deliver_later is called.

    Check out the Rails 4.2 upgrade guide for more details.

    Don’t worry though. You can still use Postmark with ActionMailer. The official Postmark Rails gem provides a drop-in integration with ActionMailer for Postmark. By using it you should be able to write your mailers as if they were regular Rails mailers without any need to manually manage Postmark connections in your code.

    P.S. I work at Wildbit (creators of Postmark). Feel free to contact us directly at any time.