# frozen_string_literal: true

if (Rails.env.staging? || Rails.env.production?) && ENV['MAILER_SMTP_SETTINGS'].present?
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = JSON.parse(ENV['MAILER_SMTP_SETTINGS']).symbolize_keys
  ActionMailer::Base.default_url_options = { host: ENV['MAILER_HOST'] }
  ActionMailer::Base.asset_host = ENV['MAILER_HOST']
end

# frozen_string_literal: true

if (Rails.env.staging? || Rails.env.production?)
  if ENV['MAILER_SMTP_SETTINGS'].present?
    ActionMailer::Base.delivery_method = :smtp
    ActionMailer::Base.smtp_settings = JSON.parse(ENV['MAILER_SMTP_SETTINGS']).symbolize_keys
    ActionMailer::Base.default_url_options = { host: ENV['MAILER_HOST'] }
    ActionMailer::Base.asset_host = ENV['MAILER_HOST']
  elsif File.file?(Rails.root.join('config/email.yml'))
    email_settings = YAML.safe_load(File.open(Rails.root.join('config/email.yml')))

    if email_settings[Rails.env].present?
      ActionMailer::Base.smtp_settings = email_settings[Rails.env].symbolize_keys
      ActionMailer::Base.delivery_method = :smtp
      ActionMailer::Base.perform_deliveries = true
    end
  end
end

Allow loading mailer config from a file
Allow loading mailer config from a file

Maintains backwards compatibility with the Capistrano deploy until the
app is moved over to the Docker-based servers.
