Sha256: 82f85d1492d162539470381cc7af0d9a6881beb5b6b75a00ae8240e83189db84

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Truemail
  module Validate
    class Smtp < Truemail::Validate::Base
      ERROR = 'smtp error'

      attr_reader :smtp_results

      def initialize(result)
        super(result)
        @smtp_results = []
      end

      def run
        return false unless Truemail::Validate::Mx.check(result)
        establish_smtp_connection
        return true if success(success_response?)
        result.smtp_debug = smtp_results
        add_error(Truemail::Validate::Smtp::ERROR)
        false
      end

      private

      def request
        smtp_results.last
      end

      def rcptto_error
        request.response.errors[:rcptto]
      end

      def establish_smtp_connection
        result.mail_servers.each do |mail_server|
          smtp_results << Truemail::Validate::Smtp::Request.new(host: mail_server, email: result.email)
          next unless request.check_port
          request.run || rcptto_error ? break : next
        end
      end

      def success_response?
        smtp_results.map(&:response).any?(&:rcptto)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
truemail-0.1.1.3 lib/truemail/validate/smtp.rb