From c340e3792224205c167d64988df7afa613b4a448 Mon Sep 17 00:00:00 2001 From: TOMITA Masahiro Date: Sun, 6 Aug 2023 12:25:46 +0900 Subject: [PATCH] Fix: send_message() with recipients as array --- lib/net/smtp.rb | 2 ++ test/net/smtp/test_smtp.rb | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index ef0150f..388c7f7 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -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} @@ -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)} diff --git a/test/net/smtp/test_smtp.rb b/test/net/smtp/test_smtp.rb index 7fb9198..4904cf7 100644 --- a/test/net/smtp/test_smtp.rb +++ b/test/net/smtp/test_smtp.rb @@ -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 @@ -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 .\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