Class: URI::SMTP
- Inherits:
-
Generic
- Object
- Generic
- URI::SMTP
- Defined in:
- lib/uri/smtp.rb,
lib/uri/smtp/version.rb
Overview
Class that adds smtp(s)-scheme to the standard URI-module.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.7.2"
Class Method Summary collapse
-
.parse(uri) ⇒ URI::SMTP
Parse
uri
and instantiate instance of URI::SMTP.
Instance Method Summary collapse
-
#auth ⇒ String?
Return mechanism of authentication (default
"plain"
). -
#decoded_userinfo(format: :string) ⇒ String, ...
Decoded userinfo formatted as String, Array or Hash.
-
#domain ⇒ String?
The host to send mail from, i.e.
-
#host_local? ⇒ Boolean
Whether or not
host
is considered local. -
#insecure? ⇒ Boolean
Whether or not the scheme indicates to skip STARTTLS.
- #open_timeout ⇒ Integer
-
#parsed_query ⇒ Hash
query
as Hash with valuesstarttls
,read_timeout
andopen_timeout
coerced. - #port ⇒ Integer
- #read_timeout ⇒ Integer
-
#starttls ⇒ false, ...
Whether or not to use
STARTTLS
. -
#tls ⇒ Boolean
(also: #tls?)
Whether or not
scheme
starts with"smtps"
. -
#to_h(format: nil) ⇒ Hash
Return Hash representing the URI.
Class Method Details
Instance Method Details
#auth ⇒ String?
Return mechanism of authentication (default "plain"
).
Only returns value when #userinfo is provided and authentication is not "none"
.
Authentication can be provided via scheme (e.g. "smtp+login://..."
) or via
query-params (e.g. "smtp://foo.org?auth=cram-md5"
). The latter takes precedence when both are provided.
A provided value of "none"
results in nil
. Other values are returned as is.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/uri/smtp.rb', line 47 def auth # net-smtp: passing authtype without user/pw raises error return nil unless userinfo return nil if parsed_query["auth"] == "none" return parsed_query["auth"] if parsed_query.has_key?("auth") return nil if scheme_auth == "none" return scheme_auth if scheme_auth "plain" end |
#decoded_userinfo(format: :string) ⇒ String, ...
Decoded userinfo formatted as String, Array or Hash.
NOTE not provided user or password result in nil
(format: :array) or absent keys (format: :hash).
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/uri/smtp.rb', line 81 def decoded_userinfo(format: :string) return if userinfo.nil? case format when :string [decoded_user, decoded_password].join(":") when :array [string_presence(decoded_user), string_presence(decoded_password)] when :hash { user: string_presence(decoded_user), password: string_presence(decoded_password) }.delete_if { |_k, v| v.nil? } else raise ArgumentError, "Unknown format #{format.inspect}. Should be one of #{%i[string array hash].inspect}." end end |
#domain ⇒ String?
The host to send mail from, i.e. the HELO
domain.
104 105 106 |
# File 'lib/uri/smtp.rb', line 104 def domain parsed_query["domain"] || fragment end |
#host_local? ⇒ Boolean
Whether or not host
is considered local.
Hostnames that are considered local have certain defaults (i.e. port 25
and no STARTTLS
).
170 171 172 |
# File 'lib/uri/smtp.rb', line 170 def host_local? %w[127.0.0.1 localhost].include?(host) end |
#insecure? ⇒ Boolean
Whether or not the scheme indicates to skip STARTTLS.
157 158 159 |
# File 'lib/uri/smtp.rb', line 157 def insecure? scheme.start_with?("smtp+insecure") end |
#open_timeout ⇒ Integer
114 115 116 |
# File 'lib/uri/smtp.rb', line 114 def open_timeout parsed_query["open_timeout"] end |
#parsed_query ⇒ Hash
query
as Hash with values starttls
, read_timeout
and open_timeout
coerced.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/uri/smtp.rb', line 176 def parsed_query @parsed_query ||= URI.decode_www_form(query.to_s).to_h .delete_if { |_k, v| !string_presence(v) } .tap do _1["read_timeout"] &&= _1["read_timeout"].to_i _1["open_timeout"] &&= _1["open_timeout"].to_i _1["starttls"] &&= case _1["starttls"] when "always", "auto" then _1["starttls"].to_sym when "false" then false else :always end end end |
#port ⇒ Integer
13 14 15 16 17 18 19 |
# File 'lib/uri/smtp.rb', line 13 def port return @port if @port return 25 if host_local? return 465 if tls? 587 end |
#read_timeout ⇒ Integer
109 110 111 |
# File 'lib/uri/smtp.rb', line 109 def read_timeout parsed_query["read_timeout"] end |
#starttls ⇒ false, ...
Whether or not to use STARTTLS
.
The possible return values (i.e. :always
, :auto
and false
) map to what net-smtp uses:
:always
useSTARTTLS
or disconnect when server does not support it.:auto
useSTARTTLS
when supported, otherwise continue unencrypted.false
don't useSTARTTLS
.
130 131 132 133 134 135 136 137 |
# File 'lib/uri/smtp.rb', line 130 def starttls return false if tls? return parsed_query["starttls"] if parsed_query.has_key?("starttls") return false if host_local? return false if insecure? :always end |
#tls ⇒ Boolean Also known as: tls?
Returns whether or not scheme
starts with "smtps"
.
140 141 142 |
# File 'lib/uri/smtp.rb', line 140 def tls !!scheme[/^smtps/] end |
#to_h(format: nil) ⇒ Hash
Return Hash representing the URI.
format
should be one of: nil
or :action_mailer
(or :am
).
Format :action_mailer
matches how ActionMailer should be configured and works around some quirks in Mail v2.8.1.
NOTE keys with nil-values are stripped.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/uri/smtp.rb', line 226 def to_h(format: nil) case format when :am, :action_mailer { address: host, authentication: auth, domain:, enable_starttls: starttls == :always, enable_starttls_auto: starttls == :auto, open_timeout:, port:, read_timeout:, tls: }.tap do unless _1[:authentication].nil? _1[:user_name] = decoded_user _1[:password] = decoded_password end # mail 2.8.1 logic is faulty in that it shortcuts # (start)tls-settings when they are false. # So we delete these flags. _1.delete(:tls) unless _1[:tls] _1.delete(:enable_starttls) unless _1[:enable_starttls] _1.delete(:enable_starttls) if _1[:tls] _1.delete(:enable_starttls_auto) unless _1[:enable_starttls_auto] _1.delete(:enable_starttls_auto) if _1[:tls] end.delete_if { |_k, v| v.nil? } else { auth:, domain:, host:, open_timeout:, port:, read_timeout:, scheme:, starttls:, tls: }.tap do unless _1[:auth].nil? _1[:user] = decoded_user _1[:password] = decoded_password end end.delete_if { |_k, v| v.nil? } end end |