Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including 'mailgun-ruby' causes action_mailer configs in an initializer to not be used #86

Closed
eebs opened this issue Mar 16, 2017 · 9 comments

Comments

@eebs
Copy link

eebs commented Mar 16, 2017

Hello, I have encountered an odd scenario where I am unable to set action_mailer configuration options if I include the mailgun-ruby gem.

I have a Rails 4.2.7.1 app, where I configure ActionMailer in an initializer. Some of the options I set are the default_url_options and asset_host, so that images in our emails have an absolute URL rather than a relative one. Without requiring mailgun-ruby, emails are generated correctly. As soon as I add gem 'mailgun-ruby' to my Gemfile, these options are no longer present at the time the email is rendered. This causes an exception to be throw by Rails stating that the default_url_options are not set.

I did some digging, and found that the root cause is the Railgun::Railtie, which adds the mailgun delivery method in before_initialize hook. I can not find any explanation either in Rails or elsewhere that would explain why adding a delivery method would cause subsequent configuration to not be used. I looked at the Rails initialization events and found nothing that points to a reason why.

I am able to work around this issue by requiring all necessary files except the Railtie. I'm not actually using the mailgun delivery method, my use case requires me to manually call Railgun.transform_for_mailgun(mail) and client.send_message('my-domain', message_payload), so adding the following to my Gemfile works:

gem 'mailgun-ruby', '~>1.1.4', require: ['mailgun', 'railgun/attachment', 'railgun/errors', 'railgun/mailer', 'railgun/message']

Do you have any thoughts on why this might be happening?

For development, my initializer is effectively this:

# config/initializers/action_mailer.rb
Rails.application.config.action_mailer.default_url_options = { host: 'localhost', :port: 3000, protocol: 'http' }
Rails.application.config.action_mailer.asset_host = 'http://localhost:3000'
Rails.application.config.action_mailer.delivery_method = :smtp
Rails.application.config.action_mailer.smtp_settings = {
  address: 'smtp.mailgun.org',
  port: 587,
  domain: 'my-domain',
  user_name: 'username',
  password: 'supersecret',
  authentication: 'plain',
  enable_starttls_auto: true,
}
Rails.application.config.action_mailer.perform_deliveries = false
@pirogoeth
Copy link
Contributor

Hey there! It looks like this is happening because requiring mailgun-ruby in turn requires railgun when Rails is defined. The solution should be simple - remove the railgun require line from the mailgun-ruby require hook. I'll take care of this today, thanks for the report :)

@eebs
Copy link
Author

eebs commented Mar 16, 2017

I'm not sure that's really what you want to do. I think requiring Railgun when Rails is defined makes sense. I'm more interested in why adding a delivery method prevents subsequent configuration from being used.

@pirogoeth
Copy link
Contributor

@eebs I am not entirely sure why the railtie is causing this issue. I will try to dig deeper in to it today.

@eebs
Copy link
Author

eebs commented Mar 17, 2017

Thanks. I'll also try and narrow things down from my end. I don't think I'm doing anything non-standard, but I'll try and create a simpler test case.

@subdigital
Copy link
Contributor

I ran into this issue as well. I worked around it by moving the config to config.action_mailer.mailgun_settings.

@duartemvix
Copy link

duartemvix commented Jan 3, 2019

I'm having something that might be related to this issue. I couldn't send e-mails using the API credentials just like instructions say so.

I was getting this after calling deliver_now on an instance of Mailer

Railgun::ConfigurationError: Config requires api_keykey from /home/matt/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mailgun-ruby-1.1.11/lib/railgun/mailer.rb:27:inblock in initialize'`

But my configurations were all set

image (On this pic, changing smtp_settings to mailgun_settings is giving another error)
Mailgun::ParseError: 765: unexpected token at 'Mailgun Magnificent API'

image

Updating this issue:

The only way to send e-mails is through using Action Mailer .deliver method. And not through .deliver_now.

I'm not getting the magnificent API error any more after I began using .deliver.

@bjminihan
Copy link

I've attempted the above with Devise and haven't been able to get the above working. Any advice on routing Devise mail through Mailgun?

@duartemvix
Copy link

duartemvix commented Jan 8, 2019

I've attempted the above with Devise and haven't been able to get the above working. Any advice on routing Devise mail through Mailgun?

I have managed to use Mailgun credentials for Devise to use them. I just had to be sure to check Devise default mailer configuration. If you configure it to what Mailer suits your needs, Devise willl use it.
image

In this pic, those values are used by default for Devise. If you uncomment them they should listen to your input.

Make sure to have this in your environment(development, or production).rb
image

@Retttro
Copy link
Contributor

Retttro commented Apr 5, 2021

Version 1.2.4 with the fix is released. Closing.
https://github.com/mailgun/mailgun-ruby/releases/tag/v1.2.4

@Retttro Retttro closed this as completed Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants