How to set up a custom host for developing an email program?

I have an application with the domain "www.mysite.com", which hosts the Rails application for Drupal and "app.mysite.com".

I have a situation where the search engine URL points to "www.mysite.com" instead of "app.mysite.com".

In devise.rb, I have the 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 the views / devise / mailer / reset _password_instructions.html.erb, the confirmation link looks something like this:

 <%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %> 

It successfully sends an email after clicking the link to the password link and works like a charm on localhost, but the URL node points to www.mysite.com instead of app.mysite.com and gives a 404 error.

How can I point it to the correct host?

+4
source share
2 answers

I did this while working after looking at the code that is described in the development wiki for sending email using the 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, checks for a subdomain hash or not. If it is present then the method precedes the subdomain in front of the host and determines the new host name of a different default name. I did not go through this process, because for some reason it did not work on rails 3.2, and I did not want to spend my time digging further because of the strict time limit.

I thought, wait, why I just do not explicitly specify the host name in the mailer url_for method instead of going through all these methods.

So, all I did was define link_to in mailer with the hash key host and pass the value as my subdomain. After that, it worked like a charm :).

In the views / devise / mailer / reset _password_instructions.html.erb, the confirmation link looks something like this:

 <%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token, :host => 'app.mysite.com') %> 
+3
source

It seems that the default behavior in Devise is sending from the main domain.

Here's a page that might help: https://github.com/plataformatec/devise/wiki/How-To:-Send-emails-from-subdomains

+4
source

Source: https://habr.com/ru/post/1413636/


All Articles