2

I have a rails app on Heroku which can send emails. I added a new feature to send email for a notification. I get this in Heroku logs but no emails are sent.

2016-07-29T19:57:58.895499+00:00 app[web.1]: [ActiveJob] [NotificationBroadcastJob] [40c7b651-3c5d-4308-b8b5-49a142d2930f]   Rendered notifications_mailer/new_message.html.erb (226.4ms)
2016-07-29T19:57:58.895630+00:00 app[web.1]: [ActiveJob] [NotificationBroadcastJob] [40c7b651-3c5d-4308-b8b5-49a142d2930f] NotificationsMailer#new_message: processed outbound mail in 246.0ms
2016-07-29T19:57:58.895741+00:00 app[web.1]: [ActiveJob] [NotificationBroadcastJob] [40c7b651-3c5d-4308-b8b5-49a142d2930f] Performed NotificationBroadcastJob from Async(default) in 377.85ms

This is notifications_mailer ruby file app/mailers/notifications_mailer.rb

class NotificationsMailer < ApplicationMailer
  def new_message(message)
    @message = message
    mail(to: message.receiver.email, subject: 'You have a new message')
  end  
end

This is how I call the new_message action in app/jobs/notifications_broadcast_job.rb

class NotificationBroadcastJob < ApplicationJob
.
.
    NotificationsMailer.new_message(personal_message).deliver_now

I use Sendgrid addon and this is from production.rb

  config.action_mailer.perform_caching = false

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  host = 'www.abcdef.com'
  config.action_mailer.default_url_options = { host: host }
  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => 'www.abcdef.com',
    :enable_starttls_auto => true
  }

This is my development.rb

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :test
  host = 'rails-tutorial-suai.c9.io'
  config.action_mailer.default_url_options = { host: host }
  config.action_mailer.perform_caching = false

I don't have worker dynos. This is my Procfile.

web: bundle exec puma -C config/puma.rb

Can anyone kindly help? Thanks

LovingRails
  • 1,565
  • 2
  • 16
  • 30

1 Answers1

1

Your call site of NotificationsMailer is in class NotificationBroadcastJob < ApplicationJob. That means that you are using ActiveJob. From the Mailer docs:

Active Job's default behavior is to execute jobs via the :async adapter. So, you can use deliver_later now to send emails asynchronously. Active Job's default adapter runs jobs with an in-process thread pool. It's well-suited for the development/test environments, since it doesn't require any external infrastructure, but it's a poor fit for production since it drops pending jobs on restart. If you need a persistent backend, you will need to use an Active Job adapter that has a persistent backend (Sidekiq, Resque, etc).

So in development it will work without a Job backend like Sidekiq/Resque. Assuming you have something like that set up on Heroku, you will need to have at least one worker dyno spun up and a worker process registered in your Procfile.

Here are the docs on Heroku, but the gist is: you will need Redis, a queue backend like Sidekiq, a worker process registered in your Procfile (e.g. worker: bundle exec sidekiq -q default), and a worker process spun up on Heroku (e.g. $ heroku ps:scale web=1 worker=1).

This also assumes you have provisioned the sendgrid addon on your Heroku app and added the required Config Vars.

A similar SO answer.

Community
  • 1
  • 1
maxbeizer
  • 847
  • 9
  • 12