How to set the ActionMailer default_url_options's :host dynamically to the request's hostname?
It is also possible to set a default host that will be used in all mailers by setting the :host option in the default_url_options hash
in an application_controller.rb
add:
class ApplicationController < ActionController::Base
def default_url_options
{ host: request.host_with_port }
end
end
Source: https://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options
Alternatively, you can pass the request
when calling the mailer function from the controller
class UserMailer < ActionMailer::Base
def welcome_email(user, request)
@user = user
@url = user_url(@user, host: request.host_with_port ) # do this for each link
mail(:to => user.email, :subject => "Welcome to My Awesome Site")
end
end
Source : https://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-with-named-routes
Change ActionMailer Email URL Host Dynamically
Okay after spending some time with it, I think I am starting to figure it out.
You got to use restful routes then you can set default_url_options[:host] = host
in the Mailer action and :only_path to true in default_url_options
for other controllers. Plus :locale
.
And in the view I have: objects_url(object)
Since I have different hostnames, I pass request.host_with_port
as a parameter from a controller to the Mailer when sending mail.
link_to
doesn't accept parameters and url_for()
can only build url from parts of it.
How to get dynamic host when sending email
You can pass the request when calling the mailer function from the controller--
class UserMailer < ActionMailer::Base
def welcome_email(user, request)
UserMailer.default_url_options[:host] = request.host_with_port #option1
@user = user
@url = user_url(@user, host: request.host_with_port ) #option2 (do this for each link)
mail(:to => user.email,
:subject => "Welcome to My Awesome Site")
end
end
In the above code request.host_with_port
is the "example.com"
for your case.
So above is the more dynamic way to provide the request host as you can see that you can pass the request when calling the mailer function from the controller.
This is the source that you can check -
Generating-urls-in-action-mailer-views.
This is also the explantion at action-mailer-default-url-options-and-request-host which is marked here for the answer to read.
How do I set default host for url helpers in rails?
asset_host
doesn't work for urls
You need to override default_url_options
in your ApplicationController
(at least in Rails 3)
http://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options
class ApplicationController < ActionController::Base
def default_url_options
if Rails.env.production?
{:host => "myproduction.com"}
else
{}
end
end
end
How to make custom host working for devise mailer?
I made it working after looking through the code which is described in devise wiki for sending email using subdomain helper method.
module SubdomainHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
host = Rails.application.config.action_mailer.default_url_options[:host]
[subdomain, host].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
All this helper method does is, check whether subdomain hash is present or not. If it's present then, the method prepends subdomain before host and define new host name else default host name. I didn't went through this process because for some reason it didn't worked on rails 3.2 and I didn't wanted to waste my time digging into it further due to strict time constraint.
I thought wait, why don't I just define the host name explicitly in mailer url_for method instead of traversing through all those methods.
So, all what I did was, defined link_to in mailer with a hash key host
and passed value as my subdomain. After that it worked like a charm :).
In views/devise/mailer/reset_password_instructions.html.erb, confirmation link looks something as shown below:
<%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token, :host => 'app.mysite.com') %>
How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis
Okay I figured it out the correct way to write it is
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
:)
Dynamic domain name in Rails action mailer while using sidekiq
One of the options would be to associate each user with a domain and use this information when sending the email in sidekiq.
How do I get an absolute URL for an asset in Rails 3.1?
It would appear that as of recently, sass-rails
now interprets the image_url
command within a scss file in the expected manner, resolving to the final location of the image in question.
Related Topics
Get Last Day of the Month in Ruby
Kernel#Gets Attempts to Read File Instead of Standard Input
Difference Between '%{}', '%Q{}', '%Q{}' in Ruby String Delimiters
Rails: Render View from Outside Controller
How to Change "Devise: Password Reset Instruction Email's Subject"
Convert Non-Breaking Spaces to Spaces in Ruby
Stack Level Too Deep Error in Ruby on Rails
Preload Has_Many Associations with Dynamic Conditions
In Ruby, How to Output JSON from Hash and Give It Line Breaks and Tabs
Ruby - Dynamically Add Property to Class (At Runtime)
Migrating from Rails 2 to Rails 3
How to Resolve Rails Model Namespace Collision
Rails: Dynamic Columns/Attributes on Models
Micropost's Comments on Users Page (Ruby on Rails)
One Liner in Ruby for Displaying a Prompt, Getting Input, and Assigning to a Variable