Skip to content

Custom Mailer Worker

Janko Marohnić edited this page Dec 5, 2021 · 1 revision

If you're using a background processing library without an Active Job adapter, or a 3rd-party service for sending transactional emails, the default two-phase setup might not be suitable.

In this case, instead of overriding #create_*_email and #send_email, override the #send_*_email methods instead, which are required to send the email immediately. For example:

# app/workers/rodauth_mailer_worker.rb
class RodauthMailerWorker
  include Sidekiq::Worker

  def perform(name, *args)
    email = RodauthMailer.public_send(name, *args)
    email.deliver_now
  end
end
# app/misc/rodauth_main.rb
class RodauthMain < Rodauth::Rails::Auth
  configure do
    # ...
    # use `#send_*_email` method to be able to immediately enqueue email delivery
    send_reset_password_email do
      enqueue_email(:reset_password, account_id, reset_password_key_value)
    end
    # ...
  end

  private

  # define custom method for enqueuing email delivery using our worker
  def enqueue_email(name, *args)
    db.after_commit do
      RodauthMailerWorker.perform_async(name, *args)
    end
  end
end
Clone this wiki locally