Search code examples
ruby-on-railsamazon-ec2rails-activejob

Rails Action Mailer with deliver_later on AWS


I have my production app running on AWS but am having issues with the active job - deliver_later on certain mailers. I am successfully sending all emails with deliver_later in development but there is something different in production. Certain mailers work with deliver_later but not my welcome mailer (welcomes new users). So I have to set deliver_now on this welcome mailer to have it actually send the email.

//doesn't work, email is not sent
UserMailer.welcome_email(self).deliver_later
//works
UserMailer.welcome_email(self).deliver_now

The log file from the server show this and nothing more when I use deliver_later:

[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: d7114-464e-4a90-9721-126650) to Async(mailers) with arguments: "UserMailer", "welcome_email", "deliver_now", #>

Any help would be appreciated. Thanks.


Solution

  • deliver_now Delivers an email at a time.

    If you use deliver_later you have to use ActiveJob runner like Sidekiq you can see this gist Sending emails with ActionMailer and Sidekiq also see this for basic Sidekiq email sent.

    Enqueues the email to be delivered through Active Job. When the job runs it will send the email using deliver_now.

    UserMailer.welcome_email(self).deliver_later(wait: 1.hour)
    

    method-i-deliver_later

    Basically, deliver_later is asynchronous. When you use this method, the email is not sent at the moment, but rather is pushed into a job's queue. If the job is not running, the email will not be sent. deliver_now will send the email at the moment, no matter what is the job's state. Here you can see the documentation for delivery methods.