Email sending with Sidekiq and ActionMailer fails in production but works in development

I’m having trouble with my Rails app sending emails in production. It works fine in dev mode but fails when deployed. I’m using Sidekiq for background jobs and Mailgun for email delivery.

Here’s what I’ve set up:

# User creation
 def sign_up
   new_member = Member.new(member_params)
   new_member.add_default_role
   new_member.set_email_confirmation
   
   if new_member.save
     MemberNotifier.delay.welcome_email(new_member.id)
     flash[:success] = 'Thanks for joining!'
     redirect_to home_path
   else
     render :new
   end
 end

# Mailer
 def welcome_email(member_id)
   @member = Member.find(member_id)
   mail(to: @member.email, subject: 'Welcome aboard!')
 end

In production, I’m getting this error:

Net::SMTPFatalError: 550 5.7.1 Relaying denied

Any ideas what could be causing this? Do I need to tweak some settings for Sidekiq or Mailgun in production? Thanks for any help!

I’ve faced similar issues before, and it can be frustrating. One thing that’s often overlooked is the sender email address. Make sure it matches the domain you’ve verified with Mailgun. Also, check if your production environment is using the correct Mailgun API key - it’s easy to accidentally use the test key instead of the live one.

Another potential culprit could be your server’s IP reputation. If you’re using a shared hosting service, the IP might be blacklisted. Consider using Mailgun’s SMTP relay instead of your server’s built-in SMTP.

Lastly, don’t forget to restart your Sidekiq workers after making any configuration changes. Sometimes the workers cache old settings and need a refresh to pick up the new ones.

If none of these solve it, you might want to temporarily log the full SMTP conversation in production to get more detailed error information. It’s helped me pinpoint tricky issues in the past.

hey mate, sounds like a classic SMTP relay issue. check ur mailgun credentials in production - they might be off. also, make sure ur domain is verified with mailgun. sometimes they block relaying if it ain’t set up right. double-check ur production.rb config too. good luck!

This issue often stems from incorrect SMTP configuration in the production environment. Verify your Mailgun settings in config/environments/production.rb. Ensure you’ve set the correct SMTP server, port, domain, and authentication credentials. Also, confirm that your sending domain is properly verified in Mailgun’s dashboard.

If those check out, examine your Sidekiq configuration. Sometimes background job settings can interfere with mailer functionality in production. Try running the mailer synchronously (without Sidekiq) to isolate the problem.

Lastly, review your Mailgun logs for any specific error messages or failed delivery attempts. They might provide more detailed insights into why the relay is being denied.