Files
openfoodnetwork/lib/spree/core/mail_settings.rb
Maikel Linke a41019fbff Simplify SMTP auth method selection
Instead of using the auth method name, let's just not supply username
and password when we don't want to authenticate. The two affected
servers AU and CA don't have credentials set anyway. This is compatible.

The specs needed changing though.
2024-04-16 16:40:50 +10:00

53 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Spree
module Core
class MailSettings
MAIL_AUTH = ['None', 'plain', 'login', 'cram_md5'].freeze
SECURE_CONNECTION_TYPES = ['None', 'SSL', 'TLS'].freeze
# Override the Rails application mail settings based on preferences
def self.init
new.override!
end
def override!
ActionMailer::Base.default_url_options[:host] ||= ENV.fetch("SITE_URL", Config.site_url)
ActionMailer::Base.smtp_settings = mail_server_settings
ActionMailer::Base.perform_deliveries = true
end
private
def mail_server_settings
settings = basic_settings.merge(user_credentials)
settings.merge(enable_starttls_auto: secure_connection?)
end
def user_credentials
{ user_name: Config.smtp_username.presence,
password: Config.smtp_password.presence }
end
def basic_settings
{ address: Config.mail_host,
domain: Config.mail_domain,
port: Config.mail_port,
authentication: }
end
def authentication
# "None" is an option in the UI but not a real authentication type.
# We should remove it from our host configurations but I'm keeping
# this check for backwards-compatibility for now.
Config.mail_auth_type.presence unless Config.mail_auth_type == "None"
end
def secure_connection?
Config.secure_connection_type == 'TLS'
end
end
end
end