Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #6 from mlandauer/support_multiple_recipients

Support multiple email recipients and multiple emails in a single session
  • Loading branch information...
commit e07538ec0d5c7cb7380aff436b0216066cbbdce9 2 parents 026aee5 + 3d9ab33
Aaron Gough authored
16 lib/mini-smtp-server/mini-smtp-server.rb
View
@@ -8,7 +8,7 @@ def initialize(port = 2525, host = "127.0.0.1", max_connections = 4, *args)
def serve(io)
Thread.current[:data_mode] = false
- Thread.current[:message] = {:data => ""}
+ reset_message
Thread.current[:connection_active] = true
io.print "220 hello\r\n"
loop do
@@ -23,8 +23,6 @@ def serve(io)
end
io.print "221 bye\r\n"
io.close
- Thread.current[:message][:data].gsub!(/\r\n\Z/, '').gsub!(/\.\Z/, '')
- new_message_event(Thread.current[:message])
end
def process_line(line)
@@ -39,7 +37,7 @@ def process_line(line)
Thread.current[:message][:from] = line.gsub(/^MAIL FROM\:/, '').strip
return "250 OK\r\n"
when (/^RCPT TO\:/)
- Thread.current[:message][:to] = line.gsub(/^RCPT TO\:/, '').strip
+ Thread.current[:message][:to] << line.gsub(/^RCPT TO\:/, '').strip
return "250 OK\r\n"
when (/^DATA/)
Thread.current[:data_mode] = true
@@ -52,6 +50,10 @@ def process_line(line)
if((Thread.current[:data_mode]) && (line.chomp =~ /^\.$/))
Thread.current[:message][:data] += line
Thread.current[:data_mode] = false
+
+ Thread.current[:message][:data].gsub!(/\r\n\Z/, '').gsub!(/\.\Z/, '')
+ new_message_event(Thread.current[:message])
+ reset_message
return "250 OK\r\n"
end
@@ -69,4 +71,10 @@ def process_line(line)
def new_message_event(message_hash)
end
+
+ private
+
+ def reset_message
+ Thread.current[:message] = {:data => "", :to => []}
+ end
end
19 test/unit/mini_smtp_server_test.rb
View
@@ -49,7 +49,24 @@ def setup
assert_difference("$messages.length") do
send_mail
end
- assert_equal "<some1@test.com>", $messages.first[:to]
+ assert_equal ["<some1@test.com>"], $messages.first[:to]
+ end
+
+ test "should support multiple recipients" do
+ send_mail($example_mail, "smtp@test.com", ["some1@test.com", "some2@test.com"])
+ assert_equal ["<some1@test.com>", "<some2@test.com>"], $messages.first[:to]
+ end
+
+ test "should support multiple emails in a single smtp session" do
+ Net::SMTP.start('127.0.0.1', 2525) do |smtp|
+ smtp.send_message("Some email data", "smtp@test.com", "some1@test.com")
+ smtp.send_message("Some more email data", "smtp2@test.com", "some2@test.com")
+ end
+ sleep 0.01
+ assert_equal 2, $messages.count
+
+ assert_equal({:data => "Some email data\r\n", :from => "<smtp@test.com>", :to => ["<some1@test.com>"]}, $messages[0])
+ assert_equal({:data => "Some more email data\r\n", :from => "<smtp2@test.com>", :to => ["<some2@test.com>"]}, $messages[1])
end
test "should store email body in message hash" do
Please sign in to comment.
Something went wrong with that request. Please try again.