Rails Devise Custom Mailer with Mailgun Ruby Gem - Constant Error Issue

I’m trying to set up a custom mailer that extends Devise’s mailer functionality and integrates with Mailgun for email delivery. I’ve got the mailgun-ruby gem installed and configured my production environment like this:

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
  api_key: ENV['MAILGUN_API_KEY'],
  domain: 'mail.example.com'
}

Then I created a custom mailer class that inherits from Devise::Mailer:

class CustomMailer < Devise::Mailer
  require 'mailgun-ruby'
  helper :application
  include Devise::Controllers::UrlHelpers
  default template_path: 'users/mailer'

  def confirmation_instructions(record, token, opts={})
    mailgun_client = MailGun::Client.new
    email_params = {
      from: "[email protected]",
      to: record.email,
      subject: "Confirm your email address"
    }

    mailgun_client.send_message email_params
    super
  end
end

I’ve commented out the mailer configuration in my devise initializer, but Rails keeps throwing an error saying it can’t find the MailGun constant as if it’s looking for it inside my CustomMailer class. What could be causing this namespace issue?

You’ve got a typo in your class name - it should be Mailgun::Client with a lowercase ‘g’, not MailGun::Client. But that’s not your main issue. You’re actually sending emails twice by calling both the manual Mailgun client AND super. Here’s what’s happening: when you set delivery_method = :mailgun in your Rails config, ActionMailer already handles all the Mailgun stuff automatically. You don’t need to create your own client. Just ditch the manual Mailgun code completely. Use your custom mailer to tweak headers, body content, or template data, then call super. This’ll fix your constant errors and stop the duplicate sends.

That constant error is happening because of case sensitivity with the gem namespace. The mailgun-ruby gem uses Mailgun (capital M, lowercase g), not MailGun like you have in your code. Change MailGun::Client.new to Mailgun::Client.new in your custom mailer. But here’s the bigger issue - you don’t actually need to manually create the Mailgun client at all. You’ve already configured Rails to use mailgun globally as the delivery method. The super call handles the actual sending through your configured settings. Just remove all that manual mailgun client code and let Devise handle the email sending through your action_mailer settings. Your code will be cleaner and you won’t have these constant resolution problems.

you’re mixing two approaches here. since you already set up mailgun as the delivery method in production.rb, devise will automatically use it for emails. that manual mailgun client code is redundant and causing your constant error. just delete the entire mailgun_client block from your custom mailer method and let rails handle it.

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