Search code examples
ruby-on-railslinuxserverlocalhosthost

rails config mailer default url options not taking effect (development environment)


I'm working on adjusting the url appearing in a mailer created with,

<%= controller_url %>

but when I change the default with the following command

config.action_mailer.default_url_options = { :host => "example.com", :protocol => 'https', :subdomain => "www"}

the url remains unchanged as

http://localhost:8000/controller

What are some possible causes for this not working? I looked pretty thoroughly through my project folder for places where the host or subdomain might be defined, but found nothing.

EDIT: It seems I needed to restart my server after making the changes.


Solution

  • I don't know how you trying but I can show my files to compare with yours.

    So my development file is /config/environments/development.rb

    config.consider_all_requests_local = true
    config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
    

    and my production file is /config/environments/production.rb

    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.default_url_options = { :host => "https://example.com" } #=> Or https://www.example.com
    config.action_mailer.perform_deliveries = true
    
    ActionMailer::Base.smtp_settings = {
        :user_name => ENV['USERNAME'],
        :password => ENV['PASSWORD'],
        :domain => 'example.com',
        :address => 'smtp.example.net',
        :port => 321,
        :authentication => :plain,
        :enable_starttls_auto => true
    }
    config.action_mailer.delivery_method = :smtp
    

    and it's working perfectly.

    Remember these:

    • Development file is working only when a project is a development mode
    • Production file is working only when a project is a production mode
    • And make sure you restarted the server after change anything on related to configuration

    You can check the logs while sending mail what's happening and for production run follow this command rails server -e production