I have app with domain 'www.mysite.com' which is hosting Drupal and 'app.mysite.com' hosting Rails app.
I have situation where the devise mailer url points to 'www.mysite.com' instead of 'app.mysite.com'.
In devise.rb, I've settings as shown below:
config.mailer = "Devise::Mailer"
ActionMailer::Base.default_url_options = { :host => 'app.mysite.com' }
And in production.rb:
config.action_mailer.default_url_options = { :host => 'app.mysite.com' }
ActionMailer::Base.smtp_settings =
{
:address => 'smtp.sendgrid.net', # SendGrid SMTP server
:domain => 'mysite.com', # SendGrid account domain name
:user_name => 'username', # SendGrid user name
:password => 'password', # SendGrid password
:enable_starttls_auto => true,
:port => 587,
:authentication => :plain,
}
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) %>
It successfully sends the email after clicking forgot password link and works like charm on localhost but the url host points to www.mysite.com
instead of app.mysite.com
and throws 404 error.
How can I point it to right host?
Looks like the default behavior in Devise is to send from the main domain.
Here's a page that may help: https://github.com/plataformatec/devise/wiki/How-To:-Send-emails-from-subdomains
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') %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With