Read more

Devise: How to send asynchronous emails

Niklas Hasselmeyer
September 28, 2023Software engineer at makandra GmbH

By default, Devise sends all emails synchronously with deliver_now.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

To change that, Devise's readme suggests overwriting the send_devise_notification method like this:

class User

  def send_devise_notification(notification, *args)
    devise_mailer.send(notification, self, *args).deliver_later
  end
  
end

However, there is one problem: When deliver_later enqueues the mail with ActiveJob, the job arguments are logged. In case of a password reset, this includes the password reset token, which should not be logged.

ActiveJob's logs can be disabled per job, so you can do this:

class ApplicationMailDeliveryJob < ActionMailer::MailDeliveryJob
  self.log_arguments = false
end
class ApplicationMailer # (or any other mailer that the devise mailer inherits from, see devise configuration)
  self.delivery_job = ApplicationMailDeliveryJob
end

With this configuration, you can send asynchronous mail without logging sensitive parameters and everyone is happy :)

Posted by Niklas Hasselmeyer to makandra dev (2023-09-28 12:50)