【问题标题】:How to change mailling url for staging environment如何更改登台环境的邮寄网址
【发布时间】:2016-04-07 06:29:11
【问题描述】:

我通过对生产服务器使用 fork 命令创建了新的暂存环境。现在对于用户注册,我向用户发送了邮件进行身份验证,现在对于登台服务器,我如何更改该邮件 url。就我而言,暂存服务器邮件地址仍然是我的生产服务器。

【问题讨论】:

    标签: ruby-on-rails ruby git heroku


    【解决方案1】:

    试试这个,

     config.action_mailer.default_url_options = { host: "example.com" }
    

    通过将 :host 选项设置为 config/application.rb 中的配置选项来设置将在所有邮件程序中使用的默认主机,请参阅此链接 (http://api.rubyonrails.org/classes/ActionMailer/Base.html)

    【讨论】:

    • 它如何在暂存环境中工作,在我的 config/environment/production.rb 中我添加了 if Rails.env == "production" config.action_mailer.default_url_options = { :host => 'getpayad. herokuapp.com' } else config.action_mailer.default_url_options = { :host => 'getpayad-staging.herokuapp.com' } end 但问题甚至在暂存环境中仍在生产中。
    • 您必须为您的应用程序配置自定义ENV,请参阅此链接它应该对您有所帮助(richonrails.com/articles/creating-a-custom-rails-environment
    【解决方案2】:

    您可以创建文件:config/initializers/action_mailer.rb,其内容如下:

    # config/initializers/action_mailer.rb
    if Rails.env.development?
    
      # Settings for mailcatcher on dev enviroment
      Rails.application.config.action_mailer.tap do |action_mailer|
        action_mailer.default_url_options = {
            host: 'dev-domain.dev',
            port: 3000
        }
    
        action_mailer.delivery_method = :smtp
        action_mailer.perform_deliveries = true
        action_mailer.raise_delivery_errors = false
        action_mailer.smtp_settings = { address: "localhost", port: 1025 }
      end
    end
    
    if Rails.env.production?
      # Define settings for Production SMTP Server
      Rails.application.config.action_mailer.tap do |action_mailer|
        action_mailer.default_url_options = {
            host: 'production-domain.com'
        }
    
        action_mailer.delivery_method = :smtp
        action_mailer.perform_deliveries = true
        action_mailer.raise_delivery_errors = true
        action_mailer.smtp_settings = {
            address: 'mail.server.com',
            port: '465',
            authentication: :plain,
            user_name: 'noreply@production-domain.com',
            password: '',
            domain: 'production-domain.com',
            enable_starttls_auto: false,
            ssl: true
        }
      end
    end
    
    if Rails.env.staging?
      # Define settings for Staging SMTP Server
      Rails.application.config.action_mailer.tap do |action_mailer|
        action_mailer.default_url_options = {
            host: 'staging-domain.com'
        }
    
        action_mailer.delivery_method = :smtp
        action_mailer.perform_deliveries = true
        action_mailer.raise_delivery_errors = true
        action_mailer.smtp_settings = {
            address: 'mail.staging-server.com',
            port: '465',
            authentication: :plain,
            user_name: 'noreply@staging-domain.com',
            password: '',
            domain: 'staging-domain.com',
            enable_starttls_auto: false,
            ssl: true
        }
      end
    end
    

    【讨论】:

      【解决方案3】:

      您可以按照here 的说明为应用程序的 URL 使用环境变量,例如APPLICATION_URL = 'http://foo.herokuapp.com' 在生产环境中和 APPLICATION_URL = 'http://foo-staging.herokuapp.com' 在暂存环境中。然后,您可以通过在代码中使用这些环境变量,在不同的环境中使用不同的 URL。

      【讨论】:

      • 请解释你的答案。
      • 我添加了更多内容。希望这是有道理的。
      猜你喜欢
      • 1970-01-01
      • 2018-04-14
      • 2017-09-12
      • 1970-01-01
      • 2021-11-30
      • 2012-01-21
      • 2015-11-01
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多