# by Bob
require 'cgi'
require 'uri'
def config_hash_from_uri(uri)
u = URI.parse(uri)
query = Hash[CGI.parse(u.query).map{ |k,v| [k, v.first] }]
h = {
username: u.user,
password: u.password,
address: u.host,
port: u.port,
domain: query['domain'],
authentication: query['authentication']&.to_sym,
enable_starttls_auto: query['enable_starttls_auto'] == 'true',
}
end
require 'pp'
pp config_hash_from_uri('smtp://bob:pass@mailhost.fart:25/?authentication=plain&domain=example.com&enable_starttls_auto=true')
pp config_hash_from_uri('smtp://bob:pass@mailhost.fart:25000/?domain=example.com&enable_starttls_auto=false')
pp config_hash_from_uri('smtp://bob:pass@mailhost.fart/?')