Skip to content

Commit

Permalink
fix: retry connections without SSL when SSL issue is encountered duri…
Browse files Browse the repository at this point in the history
…ng smtp sending
  • Loading branch information
adamcooke committed Sep 12, 2022
1 parent 6bace2c commit 0dc6824
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/postal/smtp_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ def start
end
next
end

smtp_client = Net::SMTP.new(hostname, port)
smtp_client.open_timeout = Postal.config.smtp_client.open_timeout
smtp_client.read_timeout = Postal.config.smtp_client.read_timeout

if @source_ip_address
# Set the source IP as appropriate
smtp_client.source_address = ip_type == :aaaa ? @source_ip_address.ipv6 : @source_ip_address.ipv4
end

case ssl_mode
when 'Auto'
smtp_client.enable_starttls_auto(self.class.ssl_context_without_verify)
Expand All @@ -63,9 +66,17 @@ def start
else
# Nothing
end

smtp_client.start(@source_ip_address ? @source_ip_address.hostname : self.class.default_helo_hostname)
log "Connected to #{@remote_ip}:#{port} (#{hostname})"

rescue => e
if e.is_a?(OpenSSL::SSL::SSLError) && ssl_mode == 'Auto'
log "SSL error (#{e.message}), retrying without SSL"
ssl_mode = nil
retry
end

log "Cannot connect to #{@remote_ip}:#{port} (#{hostname}) (#{e.class}: #{e.message})"
@connection_errors << e.message unless @connection_errors.include?(e.message)
smtp_client.disconnect rescue nil
Expand Down

0 comments on commit 0dc6824

Please sign in to comment.