Mailgun only delivering emails to single recipient instead of all users in Rails app

I’m having trouble with my Rails application where I want to send notifications to all registered users when a new article gets published. I’m using Mailgun as my email service but it only sends the email to one person instead of everyone on my user list. This is happening on my live server.

Here’s my mailer method:

def article_published_alert(recipients)
  @recipients = User.all
  @recipients.each do |recipient|
    mail to: recipient.email, subject: "New article published!"
  end
end

And in my articles controller:

# POST /articles
def create
  @article = current_user.articles.build(article_params)
  
  if @article.save
    NotificationMailer.article_published_alert(@users).deliver
    redirect_to @article, notice: 'Article created successfully'
  else
    render :new
  end
end

What am I doing wrong here? The email should go to all users but Mailgun only processes it for one person.

yeah thats a common mistake with actionmailer. you cant just loop mail() calls like that - only the last one executes. try using deliver_now instead of deliver too, or even better use sidekiq/delayed_job to avoid blocking your controller while sending emails to lots of users.

The issue is in your mailer method structure. When you call mail multiple times in a loop like that, only the last call actually gets processed and delivered. Each subsequent mail call overwrites the previous one rather than queuing multiple emails.

You need to restructure this to send individual emails. Instead of looping inside the mailer method, move the loop to your controller and call the mailer for each recipient separately. Change your controller to something like:

User.all.each do |user|
  NotificationMailer.article_published_alert(user).deliver
end

Then update your mailer to accept a single user:

def article_published_alert(recipient)
  @recipient = recipient
  mail to: recipient.email, subject: "New article published!"
end

This way each email gets properly queued and delivered through Mailgun. I ran into the same problem when I first started using Action Mailer and this approach fixed it completely.

Another approach worth considering is using Mailgun’s batch sending feature if you’re dealing with larger user lists. The fundamental problem others mentioned is correct - multiple mail() calls don’t work as expected in ActionMailer. However, I’d suggest also implementing some error handling around your email delivery. In production, email failures can happen for various reasons (invalid addresses, rate limits, etc.) and you don’t want one bad email to break the entire notification process. Consider wrapping your delivery calls in begin/rescue blocks and maybe add some logging to track which emails actually get sent. Also check your Mailgun dashboard logs - they’ll show you exactly what requests are being made and whether emails are getting queued properly on their end. Sometimes the issue isn’t in your code but in how the emails are configured or if you’re hitting sending limits.