lib/net/smtp.rb in net-smtp-0.3.0 vs lib/net/smtp.rb in net-smtp-0.3.1
- old
+ new
@@ -177,11 +177,11 @@
# # CRAM MD5
# Net::SMTP.start('your.smtp.server', 25
# user: 'Your Account', secret: 'Your Password', authtype: :cram_md5)
#
class SMTP < Protocol
- VERSION = "0.3.0"
+ VERSION = "0.3.1"
Revision = %q$Revision$.split[1]
# The default SMTP port number, 25.
def SMTP.default_port
@@ -249,10 +249,19 @@
@tls_verify = tls_verify
@tls_hostname = tls_hostname
@ssl_context_params = ssl_context_params
end
+ # If +true+, verify th server's certificate.
+ attr_accessor :tls_verify
+
+ # The hostname for verifying hostname in the server certificatate.
+ attr_accessor :tls_hostname
+
+ # Hash for additional SSLContext parameters.
+ attr_accessor :ssl_context_params
+
# Provide human-readable stringification of class state.
def inspect
"#<#{self.class} #{@address}:#{@port} started=#{@started}>"
end
@@ -272,16 +281,19 @@
# You cannot get valid value before opening SMTP session.
def capable_starttls?
capable?('STARTTLS')
end
+ # true if the EHLO response contains +key+.
def capable?(key)
return nil unless @capabilities
@capabilities[key] ? true : false
end
- private :capable?
+ # The server capabilities by EHLO response
+ attr_reader :capabilities
+
# true if server advertises AUTH PLAIN.
# You cannot get valid value before opening SMTP session.
def capable_plain_auth?
auth_capable?('PLAIN')
end
@@ -701,23 +713,29 @@
# Sends +msgstr+ as a message. Single CR ("\r") and LF ("\n") found
# in the +msgstr+, are converted into the CR LF pair. You cannot send a
# binary message with this method. +msgstr+ should include both
# the message headers and body.
#
- # +from_addr+ is a String representing the source mail address.
+ # +from_addr+ is a String or Net::SMTP::Address representing the source mail address.
#
- # +to_addr+ is a String or Strings or Array of Strings, representing
+ # +to_addr+ is a String or Net::SMTP::Address or Array of them, representing
# the destination mail address or addresses.
#
# === Example
#
# Net::SMTP.start('smtp.example.com') do |smtp|
# smtp.send_message msgstr,
# 'from@example.com',
# ['dest@example.com', 'dest2@example.com']
# end
#
+ # Net::SMTP.start('smtp.example.com') do |smtp|
+ # smtp.send_message msgstr,
+ # Net::SMTP::Address.new('from@example.com', size: 12345),
+ # Net::SMTP::Address.new('dest@example.com', notify: :success)
+ # end
+ #
# === Errors
#
# This method may raise:
#
# * Net::SMTPServerBusy
@@ -750,13 +768,13 @@
# it is converted to the CR LF pair. You cannot send a binary
# message with this method.
#
# === Parameters
#
- # +from_addr+ is a String representing the source mail address.
+ # +from_addr+ is a String or Net::SMTP::Address representing the source mail address.
#
- # +to_addr+ is a String or Strings or Array of Strings, representing
+ # +to_addr+ is a String or Net::SMTP::Address or Array of them, representing
# the destination mail address or addresses.
#
# === Example
#
# Net::SMTP.start('smtp.example.com', 25) do |smtp|
@@ -902,23 +920,25 @@
def ehlo(domain)
getok("EHLO #{domain}")
end
+ # +from_addr+ is +String+ or +Net::SMTP::Address+
def mailfrom(from_addr)
- getok("MAIL FROM:<#{from_addr}>")
+ addr = Address.new(from_addr)
+ getok((["MAIL FROM:<#{addr.address}>"] + addr.parameters).join(' '))
end
def rcptto_list(to_addrs)
raise ArgumentError, 'mail destination not given' if to_addrs.empty?
ok_users = []
unknown_users = []
to_addrs.flatten.each do |addr|
begin
rcptto addr
rescue SMTPAuthenticationError
- unknown_users << addr.dump
+ unknown_users << addr.to_s.dump
else
ok_users << addr
end
end
raise ArgumentError, 'mail destination not given' if ok_users.empty?
@@ -927,12 +947,14 @@
raise SMTPAuthenticationError, "failed to deliver for #{unknown_users.join(', ')}"
end
ret
end
+ # +to_addr+ is +String+ or +Net::SMTP::Address+
def rcptto(to_addr)
- getok("RCPT TO:<#{to_addr}>")
+ addr = Address.new(to_addr)
+ getok((["RCPT TO:<#{addr.address}>"] + addr.parameters).join(' '))
end
# This method sends a message.
# If +msgstr+ is given, sends it as a message.
# If block is given, yield a message writer stream.
@@ -1135,9 +1157,36 @@
end
end
def logging(msg)
@debug_output << msg + "\n" if @debug_output
+ end
+
+ # Address with parametres for MAIL or RCPT command
+ class Address
+ # mail address [String]
+ attr_reader :address
+ # paramters [Array<String>]
+ attr_reader :parameters
+
+ # :call-seq:
+ # initialize(address, parameter, ...)
+ #
+ # address +String+ or +Net::SMTP::Address+
+ # parameter +String+ or +Hash+
+ def initialize(address, *args, **kw_args)
+ if address.kind_of? Address
+ @address = address.address
+ @parameters = address.parameters
+ else
+ @address = address
+ @parameters = (args + [kw_args]).map{|param| Array(param)}.flatten(1).map{|param| Array(param).compact.join('=')}
+ end
+ end
+
+ def to_s
+ @address
+ end
end
end # class SMTP
SMTPSession = SMTP # :nodoc: