class SmtpMailer < ActionMailer::Base
  default from: 'Sycamore School <noreply@sycamoreschool.com>'
  default delivery_method: :smtp

  def send_message(smtp_config, options={})
    @options = options
    @smtp_config = smtp_config

    mail build_sender
  end

  private
    def delivery_options
      {
        user_name: @smtp_config.username,
        password: @smtp_config.decrypted_password,
        address: @smtp_config.server_address,
        port: @smtp_config.port,
        authentication: 'plain',
        enable_starttls_auto: true,
        ssl: @smtp_config.security_certificate == 'ssl'
      }
    end

    def build_sender
      send_params = {
        to: @options[:recipient],
        subject: @options[:subject],
        delivery_method_options: delivery_options
      }
      send_params[:body] = @options[:body] if @options[:body]

      send_params
    end
end
