ActionMailer sometimes not sending?

actionmailer, ruby-on-rails, ruby-on-rails-3

Solution

Each instance of the mailer can only send 1 email.

Do you have code that looks like this inside of the mailer?

@subscribers.each do |s|
  mail(
    :to => s.email,
    :subject => "Foo Foo",
    :from => "someone@example.com", 
    :template_name => 'template_name'
  ).deliver
end

This doesn't seem to work. Iterate over the subscribers outside of the mailer, and call it once:

In a controller or model:

@subscribers.each do |s|
  Notifier.send_to_subscriber(s, @comment)
end

This was my experience in Rails 3.2.2. FWIW, I think what happens is that the mail object falls out of scope.

This approach may be slow, so be sure to send emails in a separate thread.

Reference and ideas: How to send multiple emails with SendGrid?

Problem

I have a problem where ActionMailer sometimes doesn't send emails. It is reproducible and affects certain people at certain times, although what is causing it is a mystery. It fails silently, although `config.action_mailer.raise_delivery_errors = true` is set. It enters the mail() routine, claims it has rendered the template: ``` Rendered notifier/notify_comment.html.erb (0.9ms) ``` And then skips the delivery and moves onto the next one. The next one is rendered & delivered no problem. When emails are delivered, they are also printed to the console, so I can see it. The content of every email is the same, only the recipient is different. I cannot understand why this would be intermittent. How can I go about debugging this?

Original source