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?