Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make custom host working for devise mailer?

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?

like image 940
Manish Das Avatar asked Feb 21 '23 10:02

Manish Das


2 Answers

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

like image 168
evanbikes Avatar answered Feb 23 '23 05:02

evanbikes


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') %>
like image 33
Manish Das Avatar answered Feb 23 '23 06:02

Manish Das