ankane/ahoy_email

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

greena13 opened this issue · 2 comments

I am posting this as an issue because it manifests as such and those who experience similar will likely come here.

When I follow the installation steps, with email open tracking enabled, I get the following error when I try and send any email:

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

Previously, generating and sending the same emails was working fine - so it was definitely the addition of ahoy_email that raising this exception.

It turns out this is because ahoy_email generates the link for the URL of the tracking pixel using the default_options set in ApplicationMailer. Up until this point, I had only been generating links using model instances so I had been setting the configuration option Rails.application.default_url_options (but not the Rails.application.config.action_mailer.default_url_options which is by url_for without a model instance).

The fix I ended up using (inspired by this stack overflow comment) was to add the following to the ahoy_email.rb initializer file (which copies the default url options used for generating links from ApplicationRecord resources to those used for generating urls from parameters):

Rails.application.config.action_mailer.default_url_options = Rails.application.default_url_options

And for chartkick to work, it needs to be ActionMailer::Base.default_url_options, e.g.:

ActionMailer::Base.default_url_options =
    Rails.application.routes.default_url_options