def deliver!(mail)
envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
if envelope_from.blank?
raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message')
end
destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
if destinations.blank?
raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message')
end
message ||= mail.encoded if mail.respond_to?(:encoded)
if message.blank?
raise ArgumentError.new('A encoded content is required to send a message')
end
smtp = Net::SMTP.new(settings[:address], settings[:port])
if settings[:enable_starttls_auto]
if smtp.respond_to?(:enable_starttls_auto)
unless settings[:openssl_verify_mode]
smtp.enable_starttls_auto
else
openssl_verify_mode = settings[:openssl_verify_mode]
if openssl_verify_mode.kind_of?(String)
openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize
end
context = Net::SMTP.default_ssl_context
context.verify_mode = openssl_verify_mode
smtp.enable_starttls_auto(context)
end
end
end
response = nil
smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp|
response = smtp.sendmail(message, envelope_from, destinations)
end
return settings[:return_response] ? response : self
end