如何为暂存环境更改mailling url

时间:2016-04-07 06:29:12

标签: ruby-on-rails ruby git heroku

我使用fork命令创建了新的登台环境到我的生产服务器。现在对于用户注册我发送邮件给用户进行身份验证,现在对于登台服务器我如何更改该邮件网址。在我的情况下,登台服务器邮件地址仍然是我的生产服务器。

3 个答案:

答案 0 :(得分:0)

试试这个,

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

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

答案 1 :(得分:0)

您可以使用以下内容创建文件: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

答案 2 :(得分:-1)

您可以使用环境变量作为应用程序的URL,如here所述,例如APPLICATION_URL =生产环境中的“http://foo.herokuapp.com”和暂存环境中的APPLICATION_URL =“http://foo-staging.herokuapp.com”。然后,您可以在代码中使用这些环境变量,在不同的环境中使用不同的URL。