Rails ActionMailer MailGun Integration Issue - Emails Not Delivering

Problem Description

I’m having trouble getting MailGun to work with my Rails app. Gmail configuration works perfectly, but when I switch to MailGun, emails seem to send but never reach the inbox.

Working Gmail Setup

config/initializers/mail_config.rb

ActionMailer::Base.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  :user_name => "[email protected]",
  :password => "my_password",
  :authentication => "plain",
  :domain => "localhost:3000",
  :enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"

Failing MailGun Configuration

config/initializers/mail_config.rb

ActionMailer::Base.smtp_settings = {
  :address => "smtp.mailgun.org",
  :port => 587,
  :user_name => "[email protected]",
  :password => "my_mailgun_password",
  :authentication => :plain,
  :domain => "sandbox123.mailgun.org",
  :enable_starttls_auto => true
}

app/mailers/notification_mailer.rb

class NotificationMailer < ActionMailer::Base
  default from: "[email protected]"
  
  def welcome_message(user)
    @user = user
    mail(to: user.email, subject: "Welcome to our platform")
  end
end

Controller code

def signup
  @user = User.new(user_params)
  if @user.save
    NotificationMailer.welcome_message(@user).deliver
    redirect_to @user, notice: 'Account created successfully'
  else
    render :new
  end
end

The logs show the email was sent successfully with a 302 response, but nothing arrives in the recipient’s inbox. Has anyone encountered this issue before? What could be causing MailGun emails to not deliver even though Rails reports success?

check your mailgun logs first - that’s where u’ll see what’s actually happening with delivery. sandbox domains have strict limits and might drop emails without telling you. also, you’re using .deliver instead of .deliver_now which can cause timing issues in dev mode.

This is a MailGun sandbox domain issue. Sandbox domains only deliver emails to authorized recipients you’ve added in your dashboard. Rails shows successful delivery, but MailGun quietly blocks unauthorized addresses. Go to your MailGun control panel and add your test email addresses to the authorized recipients list under sandbox settings. Better yet, set up a custom domain with DNS verification - this removes all delivery restrictions. Also check your spam folder since MailGun emails often land there at first.

I experienced a similar issue when transitioning to MailGun. The problem often arises from using MailGun’s sandbox domain; it prevents emails from reaching unauthorized addresses. Although Rails indicates successful sending, MailGun may not deliver those messages. To resolve this, either authorize the recipient addresses in your MailGun dashboard or consider upgrading to a verified custom domain, which alleviates these restrictions. Additionally, reviewing MailGun’s logs can provide insights that Rails may not capture.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.