How do I configure the hostname for Rails ActionMailer?

前端 未结 7 1245
日久生厌
日久生厌 2020-12-29 06:05

I\'m working on a fairly traditional forgot password email - I want to email the user a password change token embedded in a link that they can click on in order to change th

相关标签:
7条回答
  • 2020-12-29 06:27

    Can you just do

    <%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>
    
    0 讨论(0)
  • 2020-12-29 06:28

    You probably want to set :protocol => 'https' as well, btw.

    config.action_mailer.default_url_options = { 
        :host => "portal.example.com", 
        :protocol => 'https' 
    }
    
    0 讨论(0)
  • 2020-12-29 06:40

    There is another alternative, as described in http://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/

    This solution has the advantage that it doesn't require any configuration (so less of a hassle), and works fine as long as you send emails from within controllers.

    But if you plan on sending email without going through a controller (e.g. from command line or in response to another email), you need the static configuration.

    0 讨论(0)
  • 2020-12-29 06:42

    Interestingly, I had the same issue as you did, but in unit tests (while following Michael Hartl's railstutorial). I had this line in my test.rb file, but that didn't help:

    config.action_mailer.default_url_options = { host: 'example.com', protocol: 'http' }
    

    I've also added another line like this to test.rb, and surprisingly this solved the issue

    default_url_options = { host: 'example.com', protocol: 'http' }
    
    0 讨论(0)
  • 2020-12-29 06:45

    Setting default_url_options directly is deprecated in Rails 3.1

    Use the url_for helper to create it:

    <%= link_to "click here", url_for(:controller => foo, :action => 'bar', :token => token, :host => 'www.yourdomain.com') %>
    
    0 讨论(0)
  • 2020-12-29 06:46

    Setting default_url_options directly is deprecated in Rails 3.1. Use url_for instead.

    Add parameter :protocol to override default value (http), :protocol => 'https://'. This will create url starting with "https://..." instead of default "http://"

    0 讨论(0)
提交回复
热议问题