Skip to content

Commit

Permalink
Fix: send_message() with recipients as array
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtm committed Aug 6, 2023
1 parent bfd1be8 commit c340e37
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/net/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ def any_require_smtputf8(addresses)
# * IOError
#
def send_message(msgstr, from_addr, *to_addrs)
to_addrs.flatten!
raise IOError, 'closed session' unless @socket
mailfrom from_addr, any_require_smtputf8(to_addrs)
rcptto_list(to_addrs) {data msgstr}
Expand Down Expand Up @@ -824,6 +825,7 @@ def send_message(msgstr, from_addr, *to_addrs)
# * IOError
#
def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
to_addrs.flatten!
raise IOError, 'closed session' unless @socket
mailfrom from_addr, any_require_smtputf8(to_addrs)
rcptto_list(to_addrs) {data(&block)}
Expand Down
34 changes: 33 additions & 1 deletion test/net/smtp/test_smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ def test_non_continue_auth_login
assert_equal "235", err.response.status
end

def test_send_message
port = fake_server_start
smtp = Net::SMTP.start 'localhost', port
assert_nothing_raised do
smtp.send_message("message", "sender@example.com", "rcpt1@example.com")
end
end

def test_send_message_with_multiple_recipients
port = fake_server_start
smtp = Net::SMTP.start 'localhost', port
assert_nothing_raised do
smtp.send_message("message", "sender@example.com", "rcpt1@example.com", "rcpt2@example.com")
end
end

def test_send_message_with_multiple_recipients_as_array
port = fake_server_start
smtp = Net::SMTP.start 'localhost', port
assert_nothing_raised do
smtp.send_message("message", "sender@example.com", ["rcpt1@example.com", "rcpt2@example.com"])
end
end

def test_unsuccessful_send_message_server_busy
sock = FakeSocket.new("400 BUSY\r\n")
smtp = Net::SMTP.new 'localhost', 25
Expand Down Expand Up @@ -699,13 +723,21 @@ def fake_server_start(helo: 'localhost', user: nil, password: nil, tls: false, s
else
sock.puts "535 5.7.8 Error: authentication failed: authentication failure\r\n"
end
when /\AMAIL FROM:/, /\ARCPT TO:/
sock.puts "250 2.1.0 Ok\r\n"
when "DATA"
sock.puts "354 End data with <CR><LF>.<CR><LF>\r\n"
in_data = true
when "."
sock.puts "250 2.0.0 Ok: queued as ABCDEFG\r\n"
in_data = false
when "QUIT"
sock.puts "221 2.0.0 Bye\r\n"
sock.close
servers.each(&:close)
break
else
sock.puts "502 5.5.2 Error: command not recognized\r\n"
sock.puts "502 5.5.2 Error: command not recognized\r\n" unless in_data
end
end
end
Expand Down

0 comments on commit c340e37

Please sign in to comment.